void main()
{
int (*d)[10];
d[0] = 7;
d[1]=10;
printf("%d\n",*d);
}
Run Code Online (Sandbox Code Playgroud)
它应该打印10但编译器显示如下错误:
test.c:4:7:错误:从类型'int'分配类型'int [10]'时出现不兼容的类型
请注意,我已经包含了一些错误,而不是全部.
正如由Chris指出的,d是一个指针到一个阵列.这意味着您在访问变量时不正确地使用变量,但除非您指定d指向有效数组,否则您将访问随机存储器.
更改您的程序如下:
int main(void)
{
int (*d)[10]; /* A pointer to an array */
int a[10]; /* The actual array */
d = &a; /* Make `d` point to `a` */
/* Use the pointer dereference operator (unary prefix `*`)
to access the actual array `d` points to */
(*d)[0] = 7;
(*d)[1] = 10;
/* Double dereference is okay to access the first element of the
arrat `d` points to */
printf("%d\n", **d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)