指针如何在函数之间传递变量的示例

text
阅读 37 收藏 0 点赞 0 评论 0

variable_pointers_c.txt
Pointers allow us to pass variables between functions. And actually pass variables, not pass copy of them. Because if we know the address of a variable in memory, we don't need to work with copies, we can just go to that address, and work with that variable.

As we can see from the previous snippets, '*' and '&' does like the opposite things.

'&a' get's the address of the variable 'a'. So like b = &a; // b was assigned the address of a, not the variable value of a.

'*a' get's the value stored at memory address of 'a'. So if b = *a; // b is assigned the value of the variable that is stored at the memory address of 'a'.

To get the above clear, we can use them in programs.

For example, if we want to simply swap the value of 2 integer variables, we can't simply code like this:

// Fails to swap two integers

#include <stdio.h>

void swap(int a, int b);

int main(void)
{
    int x = 1;
    int y = 2;

    printf("x is %i, y is %i\n", x, y);
    swap(x, y);
    printf("x is %i, y is %i\n", x, y);
}

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


Because functions don't actually access each other's memory spaces, they only get a copy of the value. So here the "swap" function doesn't really reach the memory space of 'x' and 'y' and change the value inside that memory space. The "swap" function only get's a copy of the value of 'x' and 'y', that's why in the end no swap happened.

The way the Stack works is that it will give some memory space for main() at the bottom of the Stack. Within this space have memory space that store 'x' and 'y'. Then when swap() function is called, it will get it's own space on top of main(). Within swap() function's memory space, there are space allocated for 'a' and 'b'. 'a' and 'b' has copies of value of 'x' and 'y'. But they are in different levels of the Stack and do not interact with each other. That's why when 'a' and 'b' values are swaped, 'x' and 'y' are not changed.


If we use '&' and '*' in the program, then we can actually reach the memory space of where 'x' and 'y' is stored, and change the value inside each space.

// Swaps two integers using pointers

#include <stdio.h>

void swap(int *a, int *b);

int main(void)
{
    int x = 1;
    int y = 2;

    printf("x is %i, y is %i\n", x, y);
    swap(&x, &y);
    printf("x is %i, y is %i\n", x, y);
}

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


So here, in "main" when call the swap() function, we pass the memory address of the 2 variables using the '&' in front of the 2 variables. So '&x' and '&y' are the memory addresses of x and y, not their values.

So '&x' and '&y' are passed to 'a' and 'b' in swap() function. 'a' and 'b' are pointers which store memory addresses of x and y. Since 'a' and 'b' are memory addresses, if we add '*' in front of 'a' and 'b' then we get the value of whatever are stored in those memory addresses.

So when we call the swap() function, this function doesn't return any value to main(). All it does is to swap the memory addresses of varialbe 'x' and 'y'. So after this swap() function is called, 'x' get's 'y' previous address and 'y' gets 'x' previous address. So that means, 'x' is pointing to where 'y' was pointing before, so that's why it gets the value of previous 'y'. And the same happens to 'y'.

This way by using '&' and '*', we actually swap the memory address of 'x' and 'y', so they would point to the other one's previous address. That's why this time the swap is succeeded.
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号