[英]Creation of a variable and subsequently increasing its memory address by 1
I created a variable and then I printed its location number. 我创建了一个变量,然后打印了它的位置号。 I then incremented the memory location by 1. I got the desired results in two cases while one case is giving an answer which could not be explained properly by my teachers.
然后,我将内存位置增加了1。在两种情况下,我得到了预期的结果,而一种情况下给出的答案是我的老师无法正确解释的。 It would be really helpful if somebody told me why the second
printf("%p\\n",&i)
is giving the same output that was given first time. 如果有人告诉我为什么第二个
printf("%p\\n",&i)
给出的输出与第一次给出的输出相同,那将非常有帮助。
int main()
{
int i=3,*x;
x=&i;
printf("%p\n",x);
printf("%p\n",&i);
printf("%p\n",&(*x));
x++;
printf("\n");
printf("%p\n",x);
printf("%p\n",&i);
printf("%p\n",&(*x));
}
0x7ffce5dc5208
0x7ffce5dc5208
0x7ffce5dc5208
0x7ffce5dc520c
0x7ffce5dc5208
0x7ffce5dc520c
Every object in a C
program has a unique fixed address during its existence. C
程序中的每个对象在其存在期间都有一个唯一的固定地址。 You store a copy of i
's address into x
. 您将
i
的地址副本存储到x
。 Then you manipulate x
to change the value it holds, it now contains another address. 然后,您操纵
x
更改其持有的值,现在它包含另一个地址。 But the manipulation of the value in x
does not move i
. 但是
x
中值的操纵不会移动i
。 The object named i
is still in the same spot. 名为
i
的对象仍在同一位置。
The address of a variable never changes during its lifetime. 变量的地址在其生命周期内不会改变。 Just because
x
starts out containing the value of that address and you then increment the value of x
doesn't mean that the address of i
changes. 仅仅因为
x
开始包含该地址的值,然后又增加x
的值,并不意味着i
的地址发生了变化。
Taking an analogy from the comments, suppose you write the address of your house on a piece of paper. 从评论中进行类比,假设您将房子的地址写在一张纸上。 Now suppose you increment the value of the house number on that sheet of paper.
现在,假设您增加那张纸上的门牌号的值。 Your home is not in a different place, you just changed the house number you wrote down.
您的房屋不在其他地方,您只是更改了写下的门牌号码。
So &i
will always have the same value, regardless of whatever value x
may contain. 因此
&i
将始终具有相同的值,而不管x
可能包含什么值。
Because the pointer value is incremented but the address of the variable i
remain same. 因为指针值增加,但变量
i
的地址保持不变。 Pointer variable is also a variable that holds the address of variable i
, changing its value won't effect the address of i
variable. 指针变量也是保存变量
i
的地址的变量,更改其值不会影响i
变量的地址。
The piece of code x = &i
copies i
's address into the x
pointer. 这段代码
x = &i
i
的地址复制到x
指针中。 It's a copy, so modifying it only modifies the value stored in x
, never will it change i
's address. 它是一个副本,因此对其进行修改只会修改存储在
x
的值,而永远不会更改i
的地址。
I'm not sure what you are trying to achieve though. 我不确定您要达到的目标。 You can't change the address of a variable.
您不能更改变量的地址。
This has nothing to do with pointers and memory address, specifically. 特别是,这与指针和内存地址无关。 Check the below program
检查以下程序
int x = 5;
int y = x;
printf (" x= %d, y = %d\n", x, y);
y++;
printf (" x= %d, y = %d\n", x, y);
Output: 输出:
x= 5, y = 5
x = 5,y = 5
x= 5, y = 6
x = 5,y = 6
Changing y
does not affect the value of x
. 更改
y
不会影响x
的值。 Same is applicable in your case, too. 同样适用于您的情况。
Simply put, x
and i
are two different variables, they have their own values. 简而言之,
x
和i
是两个不同的变量,它们具有自己的值。 Now, given the fact that x
is a pointer and assigned with the value of the address of i
, you can use x
to get the value stored at i
. 现在,假设
x
是一个指针并分配了i
的地址值,则可以使用x
来获取存储在i
的值。 But that's it. 就是这样。
This does not somehow "link" the two variables together, they still can be modified independent of each other. 这不会以某种方式将两个变量“链接”在一起,但仍然可以彼此独立地对其进行修改。 Only thing, after modifying the value of
x
, you can no longer use that to access the value stored in i
, because it does not point to the address of i
anymore. 唯一的事情是,在修改
x
的值之后,您将无法再使用它来访问i
存储的值,因为它不再指向i
的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.