swap function

This is a discussion on swap function within the C Programming forums, part of the General Programming Boards category; why this function doesnt works like this: Code: void swap2 (int *c, int *d){ int g; g=*c; c=d; *d=g; } ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    swap function

    why this function doesnt works like this:
    Code:
    void swap2 (int *c, int *d){
    	int g;
    	g=*c;
    	c=d;
    	*d=g;
    }
    and not works like this:
    Code:
    void swap2 (int *c, int *d){
    	int g;
    	g=*c;
    	c=d;
    	*d=g;
    }
    I was thinking that both of them should work? And now I cannot find out why?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You need to dereference c and d when you assign d to c, since you want to change whatever they point to. Both c and d are local to the function so changing them inside swap doesn't affect what is outside it.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Aren't both of them the same?
    c=d; /* you meant *c = *d */

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    c=d; simply sets the address in c to the address in d. Not going to change much.
    Remember that they're pointing to the values that we want to swap. So we must tell the compiler that we want to change the value c is pointing to to the value d is pointing to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    void swap2 (int *c, int *d){
    	int g;
    	g=*c;
    	c=d;
    	*d=g;
    }
    Pointers point to something.

    So line;

    c=d;
    makes the c pointer point to whatever d points to.

    And then, you do

    *d=g;
    and since c points to d, it will point to HIS value, not value you wanted because the value of *d is now g.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21