如何在C语言中将两个不同的值存储在同一内存空间中?

huangapple go评论61阅读模式
英文:

How two different values can store in one memory space in C language?

问题

在第7和第8行的代码中,变量占用相同的地址空间,但在第9和第10行,我可以看到值是不同的。我也比较了地址,它显示占用相同的地址空间。在下面的for循环中,打印数组的地址时,它从不同的地址空间分配,这是正常的,但之前为什么分配到不同的地址空间呢?为什么地址会改变?在一个地址空间中如何存储两个不同的值?

我已经粘贴了代码和输出如下:

#include <stdio.h>
void main() {
    int x = 50, *p, *q;
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    p = &x;
    q = &arr[0];
    printf("\nValue : %d", &q+1);
    printf("\nValue : %d", &p);
    printf("\nValue : %d", *(q+1));
    printf("\nValue : %d", *p);
    if (&p == &q+1) {
        printf("\nSame");
    }
    printf("\nfor loop");
    for (int i=0; i<=10; i++) {
        printf("\nValue : %d", &q[i]);
    }
}

输出:
Value : 6422292
Value : 6422292
Value : 2
Value : 50
Same
for loop
Value : 6422248
Value : 6422252
Value : 6422256
Value : 6422260
Value : 6422264
Value : 6422268
Value : 6422272
Value : 6422276
Value : 6422280
Value : 6422284
Value : 6422288

英文:

In the below code in 7th and 8th line the variables are taking same address space but in 9th and 10th line I can see the values are different. and I compared the address as well and it's showing same address space. when I'm printing the addresses of the array in the below for loop it's getting allocation from different address space which is ok but then why it was allocated to different address space previously? why the address is changed? and how in one address space two different values can store?

I have pasted the code the output the code below.

#include&lt;stdio.h&gt;
void main(){
    int x = 50, *p, *q;
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    p = &amp;x;
    q = &amp;arr[0];
    printf(&quot;\nValue : %d&quot;, &amp;q+1);
    printf(&quot;\nValue : %d&quot;, &amp;p);
    printf(&quot;\nValue : %d&quot;, *(q+1));
    printf(&quot;\nValue : %d&quot;, *p);
    if(&amp;p == &amp;q+1){
        printf(&quot;\nSame&quot;);
    }
    printf(&quot;\nfor loop&quot;);
    for(int i=0; i&lt;=10; i++){
        printf(&quot;\nValue : %d&quot;, &amp;q[i]);
    }
}
output:
Value : 6422292
Value : 6422292
Value : 2
Value : 50
Same
for loop
Value : 6422248
Value : 6422252
Value : 6422256
Value : 6422260
Value : 6422264
Value : 6422268
Value : 6422272
Value : 6422276
Value : 6422280
Value : 6422284
Value : 6422288

答案1

得分: 2

printf("\nValue : %d", &q+1);
printf("\nValue : %d", &p);
printf("\nValue : %d", *(q+1));

在第一行,你将1添加到`q`的地址,因为`&amp;q`是`q`的地址。

在最后一行,你将1添加到`q`的值,然后对其进行解引用。

将1添加到`q`的地址和将1添加到`q`的值不会给你相同的*任何东西*,所以没有理由让任何差异让你感到惊讶。
英文:
printf(&quot;\nValue : %d&quot;, &amp;q+1);
printf(&quot;\nValue : %d&quot;, &amp;p);
printf(&quot;\nValue : %d&quot;, *(q+1));

In the first line, you are adding one to the address of q since &amp;q is the address of q.

In the last line, you are adding one to the value of q and then dereferencing it.

Adding one to the address of q and one to the value of q don't give you the same anything so there is no reason any differences should surprise you.

答案2

得分: 1

变量不存储在相同的地址上。
你正在使用%d,%d是用于整数值的,因此输出具有未定义的行为。

使用%p来表示指针。

英文:

The variables are not stored on the same address.
You are using %d, %d is for integer values, thus output has undefined behaviour.

use %p for pointers.

答案3

得分: 0

&amp;q 是指针变量 q 的地址。
&amp;q+1 是在 q 之后的第一个字节的地址。

&amp;p 是指针变量 p 的地址。

if (&amp;p == &amp;q+1) 测试是否将 p 存储在 q 之后的第一个字节。编译器很可能选择将它存储在那里。或者它也可能存储在任何其他地方,这完全取决于编译器为每个变量选择地址。

英文:

&amp;q is the address of the pointer variable q.
&amp;q+1 is the address of the first byte after q.

&amp;p is the address of the pointer variable p.

if (&amp;p == &amp;q+1) tests if p is stored in the first byte after q. It is quite possible that the compiler chose to store it there. Or it could be stored anywhere else, it's all up to the compiler to select addresses for each variable.

huangapple
  • 本文由 发表于 2023年7月17日 14:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定