下面的代码在做什么?
int g[] = {9,8};
int (*j) = g;
据我了解,它创建了一个指向 2 个整数数组的指针。 但是为什么这会起作用:
int x = j[0];
这不起作用:
int x = (*j)[0];
最佳答案
括号在您的示例中是多余的。指针不关心是否涉及到一个数组——它只知道它指向一个 int
int g[] = {9,8};
int (*j) = g;
也可以改写成
int g[] = {9,8};
int *j = g;
也可以改写为
int g[] = {9,8};
int *j = &g[0];
指向数组的指针看起来像
int g[] = {9,8};
int (*j)[2] = &g;
//Dereference 'j' and access array element zero
int n = (*j)[0];
在此链接中可以很好地阅读指针声明(以及如何理解它们):http://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations
关于c++ - 指向数组 c++ 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10252837/