gpt4 book ai didi

c - 为什么两个变量共享同一个地址?

转载 作者:太空宇宙 更新时间:2023-11-04 05:09:39 25 4
gpt4 key购买 nike

在 iMac、OsX 10.5 上使用 gcc 4.0.1 版

一些变量被分配了相同的地址作为二维数组中的第二组元素。

我应该怎么做才能防止编译器以这种方式分配地址?

包含一个演示问题的程序:

#include <stdio.h>

long a[1][10];
long b;
long c;

int main()
{
printf( "Address of a[0][0]: %p\n", &a[0][0] );
printf( "Address of a[0][1]: %p\n\n", &a[0][1] );
printf( "Address of a[1][0]: %p\n", &a[1][0] );
printf( "Address of a[1][1]: %p\n\n", &a[1][1] );
printf( "Address of b: %p\n", &b );
printf( "Address of c: %p\n\n", &c );
}

输出:

Address of a[0][0]: 0x2040
Address of a[0][1]: 0x2044

Address of a[1][0]: 0x2068
Address of a[1][1]: 0x206c

Address of b: 0x2068
Address of c: 0x206c

如果需要,我有 gcc 输出的“详细”描述。

最佳答案

因为您将数组定义为long a[1][10]。这意味着您只能访问 a[0][N]

请记住,当您定义数组的大小时,您最多可以索引 N-1

关于c - 为什么两个变量共享同一个地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26015519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com