简体   繁体   English

学习期间对C指针的怀疑

[英]doubts about C pointers while studying

While studying C language for university I ran into some uncertainties about pointers, so I wrote the most simple code I could think about it. 在为大学学习C语言时,我遇到了关于指针的一些不确定性,所以我编写了我能想到的最简单的代码。

void test1(int *int_pointer) {
    *int_pointer = 5; 
}

void test2(int *int_pointer) {
    int i = 6;
    int_pointer = &i; 
}

int main(void) {
    int n = 8;
    int *p = &n;
    test1(p);
    test2(p);
}

Why if I printf("%i", * int_pointer) into test2 function it prints out 6 while printf("%i", * p) in main comes out 5 (and not 6)? 为什么如果我将printf("%i", * int_pointer)打印到test2函数中,它打印出6而printf("%i", * p)在main中出来5(而不是6)?

The second question is about the following code. 第二个问题是关于以下代码。

int main (void) {
int var1 = 10;
int * var1p = & var1;
int var2 = * var1p / 2;
int * var2p = &var2;
var1 = 40;
printf("%i", * var2p);
}

Why in this case comes out 5 and not 20 if I change var1 value and var2p points to var2 (which is equals to * var1p / 2)? 为什么在这种情况下出现5而不是20如果我改变var1值并且var2p指向var2(等于* var1p / 2)?

Thanks in advance. 提前致谢。

In the first case: 在第一种情况下:

  • You initialize a variable n and you make a p pointer to point at it. 初始化变量n并使用p指针指向它。

  • Then you give the value of the p (the address of n ) to test1() and change the value of the memory field that is pointed by int_pointer (and int_pointer has the same value as p , so it changes the value of n . 然后将pn的地址)的值int_pointer test1()并更改int_pointer指向的内存字段的值(并且int_pointerp具有相同的值,因此它会更改n的值。

  • When you call test2() and give you the same address, but in the function you change the value of the local int_pointer , so it will point to the local i , but the p stays the same. 当你调用test2()并给你相同的地址,但在函数中你改变了本地int_pointer的值,所以它将指向本地i ,但p保持不变。

So that is why in the test2() function 6 is printed (the value that is pointed by int_pointer ), and in the main() 5 is printed (the value that is pointed by p ). 这就是为什么在test2()中打印函数6(由int_pointer指向的int_pointer ),并在main() 5中打印(由p指向的值)。

The second case: 第二种情况:

  • You initialize var1 to 10, and then you make var1p to point at it. var1初始化为10,然后使var1p指向它。

  • Then you initialize var2 to half, what is pointed by var1p , so 10/2=5. 然后将var2初始化为一半, var1p指向的是var1p ,所以var1p = 5。

  • Then you initialize var2p , so it will point to var2 . 然后初始化var2p ,因此它将指向var2

  • After that, you change the value of var1 but var2 stays the same, so that is why you getting 5. 之后,您更改var1的值,但var2保持不变,这就是为什么你得到5。

var1 and var2 is two different variable, so they have their own fields in memory. var1var2是两个不同的变量,因此它们在内存中有自己的字段。 If you want 40 to be printed you should make var2p to point at var1 , like var2p = &var1 . 如果要打印40,则应使var2p指向var1 ,如var2p = &var1

test1 modifies the variable pointed by int_pointer ( main 's n ). test1修改由指向的可变int_pointermainn )。

test2 modifies the pointer itself. test2修改指针本身。 Argument variables are local to the function to which they belong, and changing them has no effect on the caller. 参数变量是它们所属的函数的本地变量,更改它们对调用者没有影响。

For example, the following has no effect on the caller either: 例如,以下内容对调用者没有影响:

void test3(int i) {
    i = 9;
}

test(n);

If you wanted to modify a pointer in the caller, you'd have to pass a pointer to the pointer. 如果要修改调用者中的指针,则必须将指针传递给指针。

void test4(int **int_pointer_pointer) {
    int *int_pointer = malloc(sizeof(int));
    *int_pointer = 10;
    *int_pointer_pointer = int_pointer;
}

int *int_pointer;
test4(&int_pointer);
printf("%d\n", *int_pointer);
free(int_pointer);

Note that returning (normally or via arguments) a pointer to a local variable makes no sense because it ceases to exists when the function returns. 请注意,返回(通常或通过参数)指向局部变量的指针是没有意义的,因为它在函数返回时不再存在。 That's why I used malloc + free instead of a local variable. 这就是我使用malloc + free而不是局部变量的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM