无法理解涉及指针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);
我能理解第3行。我知道,为int * p =&放大器; X;
表示指针 P
是在内存地址指向 X
。但我无法理解接下来的4行code的。此code的输出是:
11,17
但我无法理解怎么样?有人可以解释给我。
指针是一个变量,其值是另一个变量的地址,即内存位置的直接地址。
块引用>当你分配这样的,
为int * p =&放大器; X; //这将指向x变量的地址。
在
* P做任何更改
,它会影响记忆力。所以当你访问一个与X
它具有的价值。* P = X + Y; //这相当于X = X + Y;
然后
是
也是这样。现在
X
的值为11
。P =&安培; Y;
* P = X + Y; //它等同于Y = X + Y;
所以现在x的值
11
和y值5
。所以结果是17
。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;
means that the pointerp
is pointing at the memory address ofx
. But I can't understand the next 4 lines of the code. 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, i.e., 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. So while you are accessing that with thex
it have the value.*p=x+y; // It's equivalent x=x+y;
Then
y
also like this.Now
x
have the value11
.p=&y;
*p=x+y;// It's equivalent to y=x+y;
so now x have the value
11
and y value5
. So the result is17
.这篇关于无法理解涉及指针C程序中的一小部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!