gpt4 book ai didi

c++ - 编译后数组和指向数组的指针之间的区别?

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:49 27 4
gpt4 key购买 nike

void m() {
char a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
char(*c)[3][3] = (char (*)[3][3])a;
printf("%d\n", *c[0][0]);
}

例如,在这个函数中,变量a 指向内存中连续9个整数的位置。

但是 c 呢? c 是否指向内存中的某个位置,该位置指向内存中连续保存 9 个整数的位置?

那么,从技术上讲,c 是单层指针还是双层指针?

我上面说的不应该是真的吗?当我执行以下功能时怎么会出现:

void m() {
char a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
char(*c)[3][3] = (char (*)[3][3])a;
printf("%d\n", *c[0][0]);
printf("a...%p\nc...%p\n", a, c);
}

a 和 c 都指向相同的位置?不应该c是双层指针,a是指向内存中某个位置的指针吗?

最佳答案

the variable a points to a location in memory with 9 integers in a row.

没有。变量a 内存中的一个位置,一行中有 9 个整数。 a 只是该位置的名称。

Does c point to a location in memory which points to a location in memory that holds 9 integers in a row?

没有。 c 内存中的一个位置,它指向内存中连续保存 9 个整数的位置。 c 是包含指针的位置的名称。

So, technically, is c a single layer pointer or a double layer pointer?

单例。

关于c++ - 编译后数组和指向数组的指针之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25518171/

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