开发者

pointer in c and the c program

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)
开发者_Python百科{
    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...


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.


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:

What are the barriers to understanding pointers and what can be done to overcome them?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜