views:

127

answers:

3

Hello, I am studying pointers and I encountered this program:

#include <stdio.h>

void swap(int *,int *);

int main()
{
    int a=10;
    int b=20;
    swap(&a,&b);
    printf("the value is %d and %d",a,b);
    return 0;
}

void swap(int *a,int*b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
    printf("%d and%d\n",*a,*b);
}

Can any one tell me why this main function return the value reversed? The thing I understood till now is that the function call in C does not affect the main function and it's values.

I also want to know how much the space a pointer variable occupied like integer have occupied the 2 bytes and the various application use and advantages of the pointer...

+5  A: 

You are correct: a function in C cannot directly affect the values in the caller. But the values that are being given to swap() are not the values of a and b. Rather, they are pointers to those values--basically, the address in memory where a and b live. Since swap has those addresses, it can then write to those memory locations and effectively change a and b.

Good luck with pointers, by the way. It's the hardest thing about C to get if you're coming from a context that doesn't have them. I came to C by way of Perl, and I broke my brain on pointers several times. But it'll open up a whole new world once you understand them.

JSBangs
Interesting, since Perl has references, which are quite similar. I *got* pointers figuring out how to access the Win32 API and doing low-level COM stuff from VB6.
Daren Thomas
Actually, understanding Perl's references was the key to getting C pointers. References are very similar, but a little more user-friendly.
JSBangs
+2  A: 

You are passing the addresses of a and b to swap function which has two pointers now pointing at a and b. The value of a is stored in int t. the value of b is stored then in a, and the value that was stored in a (now in t) is set to b.

Once the function completes the values are swapped, hence the change in main.

If you were to pass the actual values of a, and b to swap rather then the addresses you will notice no change upon returning to main. This is the power of pointers and setting the address of a variable to the pointer.

The pointer now can change the value of a variable because it has it's address. Think of it like giving your house key to your neighbor. Your neighbor can now get into your house and change the arrangement of anything in your house, as he / she has the key (the address of the variable). Without the key the neighbor cannot get into your house (example of pass by value) and without the key cannot make any arrangements to the house.

Draw pictures they usually help. Here is an awesome explanation given on stackoverflow:

http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome

The example given in the link is not directly for C/C++ but it can apply to any language that may support changing of values via pointers / addresses.

JonH
+1 for the link to that awesome answer
Earlz
The link to the awesome _answer_ is http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome/5754#5754.
Daniel Daranas
@Daniel Daranas - That's one thing I cannot figure out how to do on SO. How do you go directly to one answer in a thread of answers ?
JonH
Do you just go to their profile and look for the answer and then link it ?
JonH
@JonH: No, it's easier. Pretend that you're already viewing the interesting answer. Right at its bottom, you'll see the following texts (left to right): **link|edit|flag _... some white space..._ edited May 4 '09 at 9:21**. Put your mouse over the "link" text, the leftmost one. That is a real link of each answer to itself. Then you can just right-click and "copy link location". Handy!
Daniel Daranas
@Daniel Daranas - Cool never noticed that :). Thank you!
JonH
+1  A: 

The function receives a copy of what's passed (aka "pass by value"), so it can't affect the original of what was passed. In this case, what's being passed is a pointer. The function isn't changing the pointer (which is what was passed) in main, but it is changing what that pointer referred to.

Jerry Coffin