10

I'm new to C and still trying to grasp the concept of pointers. I know how to write a swap function that works...I'm more concerned as to why this particular one doesn't.

void swap(int* a, int* b)
{
 int* temp = a;
 a = b;
 b = temp;
}

int main()
{
 int x = 5, y = 10;
 int *a = &x, *b = &y;
 swap(a, b);
 printf(“%d %d\n”), *a, *b);
}
2
  • 5
    The obvious question would be, what's different between this implementation and your implementation that works? (I know what the problem with this one is, I'm just trying to help you find it.) [Of course, all the eager beavers on SO are all to willing to jump straight to the answer.] Oct 1, 2010 at 1:22
  • 2
    A solid crash lesson on pass by value. Thanks ppl. Oct 1, 2010 at 2:19

12 Answers 12

30

You're missing *s in the swap function. Try:

void swap(int* a, int* b)
{
 int temp = *a;
 *a = *b;
 *b = temp;
}

That way, instead of just swapping the pointers, you're swapping the ints that the pointers are pointing to.

12

Your swap() function does work, after a fashion - it swaps the values of the variables a and b that are local to swap(). Unfortunately, those are distinct from the a and b in main() - so you don't actually see any effect from swapping them.

0
4

When thinking about pointers, you need to be clear on a few abstractions.

An object in memory. This can be of any type (and size). An integer object, for example, will occupy 4 bytes in memory (on 32 bit machines). A pointer object will occupy 4 bytes in memory (on 32 bit machines). As should be obvious, the integer object holds integer values; a pointer object holds addresses of other objects.

The C programming language lets symbols (variables) represent these objects in memory. When you declare,

int i;

the symbol (variable) i represents some integer object in memory. More specifically, it represents the value of this object. You can manipulate this value by using i in the program.

&i will give you the address of this object in memory.

A pointer object can hold the address of another object. You declare a pointer object by using the syntax,

int* ptr;

Just like other variables, the pointer variable represents the value of an object, a pointer object. This value just happens to be an address of some other object. You set the value of a pointer object like so,

ptr = &i;

Now, when you say ptr in the program, you are referring to its value, which is the address of i. But if you say *ptr, you are referring to not the value of ptr, but rather the value of the object whose address is in ptr i.e. i.

The problem with your swap function is that you are swapping values of pointers, not the values of objects that these pointers hold addresses for. To get to the values of objects, you would have to use *ptr.

2

C is a pass-by-value language. Your swap routine doesn't dereference the pointers passed to it, so from main's perspective nothing has happened.

1

The pointers are passed by value. This means a & b are still a and b when the come back from the function;

try something like this

void swap(int* a, int* b)
{
 int temp = *a;
 *a = *b;
 *b = temp;
}
1

The right way to do it:

void swap(int* a, int* b)
{
    int temp = *a;  // Temp is set to the value stored at a (5)
    *a = *b;        // value stored at a is changed to the value stored at b (10)
    *b = temp;      // value stored in address b is changed to 5. 
}
1

It does swap. It swaps local pointers a and b inside swap function. It swaps them perfectly fine, as it should.

If you want to swap the values these pointers are pointing to, you should re-implement your swap function accordingly, i.e. make it swap the pointed values, not the pointers.

1

Umm maybe using this

void swap(int** a, int** b)
{
 int** temp = a;
 a = b;
 b = temp;
}

int main()
{
 int x = 5, y = 10;
 int *a = &x, *b = &y;
 swap(&a, &b);
 printf(“%d %d\n”), *a, *b);
}
1

Without using a third variable (temp)

void swap(int* a,int* b)
{ 
 // a = 10, b = 5;
  *a = *a + *b;  // a now becomes 15
  *b = *a - *b;  // b becomes 10
  *a = *a - *b;  // a becomes 5
}
0

zildjohn1's answer is the easiest and clearest way to do it. However if you insist on swapping the pointers, then you have to pass the pointer to the pointer because the pointer itself is passed by value.

-1

You need to send the address of a and b for swap function so while calling swap function you must call ass swap (&a,&b) So that you pass the address, and alter the address

-1
#define SWAP(a,b) ((a)=(b)+(a),(b)=(a)-(b),(a)=(a)-(b))

Works good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.