Results 1 to 4 of 4

Thread: Exercise pointer and function in C

  1. #1
    Join Date
    Nov 2008
    Posts
    1,259

    Exercise pointer and function in C

    I am writing a program for "swap" using pointers that accepts the values of two variables and write a function in which we take two integers and then display them by swapping the values. Here's the solution, but I can not compile

    Code:
    /*Swap*/ 
    
    #include<stdio.h>; 
    #include<conio.h>; 
    void swap (int *x,int *y){ 
    int val; 
    val=*x; 
    *x=*y; 
    *y=val; 
    } 
    void main(){ 
    int a;int b; 
    clrscr(); 
    scanf("%d%d,&a,&b); 
    swap(a,b); 
    printf("a=%d,b=%d",a,b); 
    do{} 
    while(kbhit()==0); 
    }

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: Exercise pointer and function in C

    swap( &a, &b );

    you pass an address not a value.

    After conio, clrscr and scanf gives envy to vomit

  3. #3
    Join Date
    Feb 2008
    Posts
    194

    Re: Exercise pointer and function in C

    Quote Originally Posted by fellah View Post
    swap( &a, &b );

    you pass an address not a value.

    After conio, clrscr and scanf gives envy to vomit
    It is a start.
    You could also notice the prototype hand, after the #include

    Code:
    /*swap*/
    #include<stdio.h>
    #include<conio.h>
    void swap(int *x,int *y)
    {
      int val;
      val=*x;
      *x=*y;
      *y=val;
    }
    int main(void)
    {
      int a;
      int b;
      clrscr();
      scanf("%d%d,&a,&b);
      swap(&a,&b);
      printf("a=%d,b=%d",a,b);
      do{}
      while(kbhit()==0);
      return 0;
    }

  4. #4
    Join Date
    May 2008
    Posts
    945

    Re: Exercise pointer and function in C

    This is sufficient:

    Code:
    /*swap*/
    #include <stdio.h>
    void swap (int *x, int *y)
    {
       int val = *x;
       *x = *y;
       *y = val;
    }
    int main (void)
    {
       int a;
       int b;
       if (scanf ("%d%d", &a, &b) == 2)
       {
          printf ("a = %d, b = %d\n", a, b);
          swap (&a, &b);
          printf ("a = %d, b = %d\n", a, b);
       }
       return 0;
    }

Similar Threads

  1. Changing Stylus pointer to normal Pointer in Tablet PC
    By Kelley in forum Portable Devices
    Replies: 3
    Last Post: 21-06-2011, 07:22 PM
  2. Parameter function and generic pointer
    By Rubero in forum Software Development
    Replies: 7
    Last Post: 25-09-2010, 09:04 PM
  3. Pointer parameter in a function
    By Chrisch in forum Software Development
    Replies: 4
    Last Post: 14-12-2009, 10:57 PM
  4. Passing function pointer as argument
    By Allan.d in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 11:43 AM
  5. Problem in a function pointer
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 23-05-2009, 10:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,388,440.39296 seconds with 16 queries