简体   繁体   English

无法理解涉及指针的C程序的一小部分

[英]Can't understand small part of a C program involving pointers

int x = 5;
int y = 6;
int *p = &x;
*p = x + y;
p = &y;
*p = x + y;
printf("%d,%d", x, y);

I can understand the first 3 lines. 我可以理解前三行。 I know that int *p = &x; 我知道int *p = &x; means that the pointer p is pointing at the memory address of x . 表示指针p指向x的存储地址。 But I can't understand the next 4 lines of the code. 但是我看不懂代码的下4行。 The output for this code is: 该代码的输出为:

 11,17

But I unable to understand how? 但是我不明白怎么办? Can someone explain it to me. 有人可以向我解释。

A pointer is a variable whose value is the address of another variable, ie, direct address of the memory location. 指针是一个变量,其值是另一个变量的地址,即存储位置的直接地址。

When you are assigning like this, 这样分配时,

 int *p=&x;// It will point to the address of x variable. 

Any change done in the *p , It will affect the memory. *p所做的任何更改都会影响内存。 So while you are accessing that with the x it have the value. 因此,当您使用x访问它时,它具有值。

 *p=x+y; // It's equivalent x=x+y;

Then y also like this. 然后y也很喜欢这一点。

Now x have the value 11 . 现在x的值为11 p=&y;

  *p=x+y;// It's equivalent to y=x+y;

so now x have the value 11 and y value 5 . 所以现在x的值为11 ,y的值为5 So the result is 17 . 因此,结果为17

Let's analyze the code line by line, shall we? 让我们逐行分析代码,对吧?

Before going into the answer, a small info, * is called the indirection or dereference operator, which is used to access the value at a memory address . 在给出答案之前,将一个小信息*称为间接取消引用运算符,该运算符用于访问内存地址处

So, per your code: 因此,根据您的代码:

  • *p = x + y; add x and y and store the value at the memory location pointed by p [ie the address of x ]. xy相加,然后将值存储在p指向存储位置中 (即x的地址)。 . So, this way, the actual value of x is getting modified. 因此,这样, x的实际值将被修改。 now, x holds x+y or 5+6 or 11 . 现在, x持有x+y5+611

  • p = &y; same logic as int *p = &x; int *p = &x;相同的逻辑

  • *p = x + y; same logic as the first point. 与第一点相同的逻辑。 remember, x value got modified in the first occasion, so the latest x value will be considered. 请记住, x值是在第一次修改的,因此将考虑最新的x值。 So, modified y will be 11 + 6 or 17 . 因此,修改后的y将为11 + 617

  • printf("%d,%d", x, y); Now don't tell me you did not understand this line. 现在,不要告诉我您不了解这一行。 :-) :-)

      *p = x + y; 

So now p will point the address of x, and (*p) will point the value stored in the x. 因此,现在p将指向x的地址,而(* p)将指向x中存储的值。 so your changing the value of x , in its memory location. 因此,您可以在内存位置更改x的值。

       p = &y;

Now your assigning the address of y to p. 现在,您将y的地址分配给p。

       *p = x + y;

Now you change the value of y using its memory address. 现在,您可以使用其内存地址更改y的值。

In fact this code snippet 实际上,此代码段

int x = 5;
int y = 6;
int *p = &x;
*p = x + y;
p = &y;
*p = x + y;
printf("%d,%d", x, y);

you may rewrite the following way without using pointers that it would be more clear 您可以重写以下方式而无需使用指针,这样会更清楚

int x = 5;
int y = 6;
x = x + y;
y = x + y;
printf("%d,%d", x, y);

So at first x is set to the sum of x + y and will be equal to 11 and then y is set to the sum of updated x (that now is equal to 11) and y and will be equal to 17 . 因此,首先将x设置为x + y和,将等于11 ,然后将y设置为更新的x (现在等于11)和y和,并将等于17

The only difference that instead of using directly x and y the first code snippet uses pointers. 唯一的区别在于,第一个代码段不是直接使用xy ,而是使用指针。

Thus 从而

statement x = x + y; 陈述x = x + y; is replaced with combination of these two statements 替换为这两个语句的组合

int *p = &x;
*p = x + y;

and statement 和声明

y = x + y;

is replaced with combination of these two statements 替换为这两个语句的组合

p = &y;
*p = x + y;

That is at first pointer p is assigned the address of x 也就是说,首先为指针p分配了x的地址

int *p = &x;

and the object (that is x) is assigned the sum of x + y 并为对象(即x)分配了x + y的和

*p = x + y;

The same is repeated for varaiable y and pointer p. 对于变量y和指针p重复相同的操作。

*p = x + y

Dereference pointer p, assign value of x + y to the memory that p is pointing to. 解引用指针p,将x + y值分配给p指向的内存。 Equivalent to x = x + y . 等效于x = x + y x is equal to x + y now, which is 5 + 6 = 11 . x现在等于x + y ,即5 + 6 = 11

p = &y

Assign the the address of y to pointer p. 将y的地址分配给指针p。 Pointer p points to memory of y in other word. 指针p指向换句话说y的存储。

*p = x +y

Dereference pointer p, assign value of x + y to the memory that p is pointing to. 解引用指针p,将x + y值分配给p指向的内存。 Equivalent to y = x + y . 等效于y = x + y y is equal to x + y now, which is 11 + 6 = 17 . y现在等于x + y,即11 + 6 = 17

At the end, x = 11 , y = 17 . 最后, x = 11y = 17

Hope it helps. 希望能帮助到你。

To better understand pointers and related ampersand and asterisk symbols i propose to this basic statements: 为了更好地理解指针和相关的与号和星号,我建议使用以下基本声明:
lets assume: 让我们假设:

int * p;
int x;

1) Pointer is an address. 1)指针是一个地址。 That means that in p is an address in memory where integer value is stored 这意味着在p中是内存中存储整数值的地址
2) when *p is used the asterisk symbol can be "translated" as "value at the address"; 2)当使用* p时,可以将星号符号“翻译”为“地址值”; This means that when we used *p we want take a value that resides in address p; 这意味着当我们使用* p时,我们想要取一个位于地址p中的值;
3) when &x is used the ampersand symbol can be "translated" as "address of"; 3)当使用&x时,“&”符号可以“翻译”为“的地址”; This means that we want to take address of x; 这意味着我们要取x的地址;

thus operations: 因此操作:
0) p = x; 0)p = x; would not be correct; 是不正确的;
1) *p = x; 1)* p = x; is correct (if p is a valid address) and value at address p is now same as value x; 是正确的(如果p是有效地址),并且地址p的值现在与值x相同;
2) p = &x; 2)p =&x; is correct, and *p == x; 是正确的,并且* p == x; in this case also you need to understand, that p and &x is the same address. 在这种情况下,您还需要了解p和&x是相同的地址。 so if you change either *p or x, the other one will have same value as well. 因此,如果您更改* p或x,则另一个也将具有相同的值。

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

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