gpt4 book ai didi

c - C变量声明中的括号是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 16:22:31 25 4
gpt4 key购买 nike

谁能解释一下这是什么意思?

int (*data[2])[2];

最佳答案

括号有什么用?

在 C 括号中 [] 的优先级高于星号 *

来自维基百科的很好的解释:

To declare a variable as being a pointer to an array, we must make use of parentheses. This is because in C brackets ([]) have higher precedence than the asterisk (*). So if we wish to declare a pointer to an array, we need to supply parentheses to override this:

double (*elephant)[20];

This declares that elephant is a pointer, and the type it points at is an array of 20 double values.

To declare a pointer to an array of pointers, simply combine the notations.

int *(*crocodile)[15];

Source .

以及您的实际情况:

int (*data[2])[5];

数据是一个包含 2 个元素的数组。每个元素都包含一个指向 5 个整数数组的指针。

所以你可以在代码中使用你的“数据”类型:

int (*data[2])[5];
int x1[5];
data[0] = &x1;
data[1] = &x1;

data[2] = &x1;//<--- out of bounds, crash data has no 3rd element
int y1[10];
data[0] = &y1;//<--- compiling error, each element of data must point to an int[5] not an int[10]

关于c - C变量声明中的括号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/244680/

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