gpt4 book ai didi

c - (*ptr)[10] 是什么意思?

转载 作者:行者123 更新时间:2023-12-02 07:05:25 25 4
gpt4 key购买 nike

void main()
{
int (*d)[10];
d[0] = 7;
d[1]=10;
printf("%d\n",*d);
}

它应该打印 10 但编译器显示如下错误:

test.c:4:7: error: incompatible types when assigning to type ‘int[10]’ from type ‘int’

请注意,我包含了一些错误,而不是全部。

最佳答案

正如克里斯所说,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;
}

关于c - (*ptr)[10] 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12739743/

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