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

时间:2022-08-26 23:18:48

Can someone explain what this means?

有人能解释一下这是什么意思吗?

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

5 个解决方案

#1


37  

What are the parentheses for?

圆括号代表什么?

In C brackets [] have a higher precedence than the asterisk *

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

Good explanation from Wikipedia:

好从*的解释:

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:

要将一个变量声明为指向数组的指针,我们必须使用圆括号。这是因为在C方括号中([])的优先级高于星号(*)。所以如果我们想声明一个指向数组的指针,我们需要提供圆括号来覆盖这个:

double (*elephant)[20];

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

这声明elephant是一个指针,它指向的类型是一个包含20个双值的数组。

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

要声明指向指针数组的指针,只需组合表示法。

int *(*crocodile)[15];

Source.

源。

And your actual case:

和你实际的例子:

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

data is an array of 2 elements. Each element contains a pointer to an array of 5 ints.

数据是两个元素的数组。每个元素都包含一个指向5英寸数组的指针。

So you you could have in code using your 'data' type:

所以你可以用你的“数据”类型来编写代码:

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]

#2


15  

There is a very cool program called "cdecl" that you can download for Linux/Unix and probably for Windows as well. You paste in a C (or C++ if you use c++decl) variable declaration and it spells it out in simple words.

有一个非常酷的程序叫做“cdecl”,你可以下载到Linux/Unix上,也可以下载到Windows上。你可以用C(或c++)的变量声明,用简单的单词拼写出来。

#3


4  

If you know how to read expressions in C, then you're one step away from reading complicated declarations.

如果您知道如何在C中读取表达式,那么就离读取复杂的声明仅一步之遥了。

What does

什么

char *p;

really mean? It means that *p is a char. What does

的真正意思是什么呢?它表示*p是一个char。什么

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

mean? It means that (*data[x])[y] is an int (provided 0 <= x < 2 and 0 <= y < 5). Now, just think about what the implications of that are. data has to be... an array of 2... pointers... to arrays of 5... integers.

的意思吗?它的意思是(*data[x])[y]是一个int(假设0 <= x < 2, 0 <= y < 5)。数据必须…一个数组的2…指针…数组中的5个…整数。

Don't you think that's quite elegant? All you're doing is stating the type of an expression. Once you grasp that, declarations will never intimidate you again!

你不觉得那很优雅吗?您所做的就是声明表达式的类型。一旦你明白了这一点,宣言就再也不会恐吓你了!

The "quick rule" is to start with the variable name, scan to the right until you hit a ), go back to the variable name and scan to the left until you hit a (. Then "step out" of the pair of parentheses, and repeat the process.

“快速规则”是从变量名开始,向右扫描直到你到达a),返回到变量名,然后扫描到左边直到你打到a(。然后“退出”这对括号,然后重复这个过程。

Let's apply it to something ridiculous:

让我们把它应用到一些荒谬的事情上:

void **(*(*weird)[6])(char, int);

weird is a pointer to an array of 6 pointers to functions each accepting a char and an int as argument, and each returning a pointer to a pointer to void.

weird是一个指向6个指针的数组的指针,每个函数都接受一个char和一个int作为参数,每个函数都返回一个指向void的指针。

Now that you know what it is and how it's done... don't do it. Use typedefs to break your declarations into more manageable chunks. E.g.

既然你知道了它是什么,以及它是如何做到的……不要这样做。使用typedef将声明分解为更易于管理的块。如。

typedef void **(*sillyFunction)(char, int);

sillyFunction (*weird)[6];

#4


-1  

data[2] - an array of two integers

数据[2]-一个由两个整数组成的数组

*data[2] - a pointer to an array of two integers

*数据[2]-一个指向两个整数数组的指针。

(*data[2]) - "

(*数据[2])——“

(*data[2])[2] - an array of 2 pointers to arrays of two integers.

(*data[2])[2] -一个指向两个整数数组的两个指针的数组的数组。

#5


-1  

If you have an array:

如果你有一个数组:

int myArray[5];
int * myArrayPtr = myArray;

Would be perfectly reasonable. myArray without the brackets is a pointer to an int. When you add the brackets its like you deference the pointer myArray. You could write...

是完全合理的。没有括号的myArray是指向int的指针。当你添加括号时,它就像你对指针myArray的尊重一样。你可以写…

myArrayPtr[1] = 3;

Which is perfectly reasonable. Using parenthesis just makes things harder to read and understand IMO. And they show that people don't understand pointers, arrays, and pointer arithmetic, which is how the compiler or linker goes from one to the other. It seems with parenthesis you do get bounds checking though.

这是完全合理的。使用括号只会使东西更难读懂。它们表明人们不理解指针、数组和指针运算,这是编译器或链接器从一个到另一个的方式。在括号里,你可以检查边界。

#1


37  

What are the parentheses for?

圆括号代表什么?

In C brackets [] have a higher precedence than the asterisk *

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

Good explanation from Wikipedia:

好从*的解释:

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:

要将一个变量声明为指向数组的指针,我们必须使用圆括号。这是因为在C方括号中([])的优先级高于星号(*)。所以如果我们想声明一个指向数组的指针,我们需要提供圆括号来覆盖这个:

double (*elephant)[20];

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

这声明elephant是一个指针,它指向的类型是一个包含20个双值的数组。

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

要声明指向指针数组的指针,只需组合表示法。

int *(*crocodile)[15];

Source.

源。

And your actual case:

和你实际的例子:

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

data is an array of 2 elements. Each element contains a pointer to an array of 5 ints.

数据是两个元素的数组。每个元素都包含一个指向5英寸数组的指针。

So you you could have in code using your 'data' type:

所以你可以用你的“数据”类型来编写代码:

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]

#2


15  

There is a very cool program called "cdecl" that you can download for Linux/Unix and probably for Windows as well. You paste in a C (or C++ if you use c++decl) variable declaration and it spells it out in simple words.

有一个非常酷的程序叫做“cdecl”,你可以下载到Linux/Unix上,也可以下载到Windows上。你可以用C(或c++)的变量声明,用简单的单词拼写出来。

#3


4  

If you know how to read expressions in C, then you're one step away from reading complicated declarations.

如果您知道如何在C中读取表达式,那么就离读取复杂的声明仅一步之遥了。

What does

什么

char *p;

really mean? It means that *p is a char. What does

的真正意思是什么呢?它表示*p是一个char。什么

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

mean? It means that (*data[x])[y] is an int (provided 0 <= x < 2 and 0 <= y < 5). Now, just think about what the implications of that are. data has to be... an array of 2... pointers... to arrays of 5... integers.

的意思吗?它的意思是(*data[x])[y]是一个int(假设0 <= x < 2, 0 <= y < 5)。数据必须…一个数组的2…指针…数组中的5个…整数。

Don't you think that's quite elegant? All you're doing is stating the type of an expression. Once you grasp that, declarations will never intimidate you again!

你不觉得那很优雅吗?您所做的就是声明表达式的类型。一旦你明白了这一点,宣言就再也不会恐吓你了!

The "quick rule" is to start with the variable name, scan to the right until you hit a ), go back to the variable name and scan to the left until you hit a (. Then "step out" of the pair of parentheses, and repeat the process.

“快速规则”是从变量名开始,向右扫描直到你到达a),返回到变量名,然后扫描到左边直到你打到a(。然后“退出”这对括号,然后重复这个过程。

Let's apply it to something ridiculous:

让我们把它应用到一些荒谬的事情上:

void **(*(*weird)[6])(char, int);

weird is a pointer to an array of 6 pointers to functions each accepting a char and an int as argument, and each returning a pointer to a pointer to void.

weird是一个指向6个指针的数组的指针,每个函数都接受一个char和一个int作为参数,每个函数都返回一个指向void的指针。

Now that you know what it is and how it's done... don't do it. Use typedefs to break your declarations into more manageable chunks. E.g.

既然你知道了它是什么,以及它是如何做到的……不要这样做。使用typedef将声明分解为更易于管理的块。如。

typedef void **(*sillyFunction)(char, int);

sillyFunction (*weird)[6];

#4


-1  

data[2] - an array of two integers

数据[2]-一个由两个整数组成的数组

*data[2] - a pointer to an array of two integers

*数据[2]-一个指向两个整数数组的指针。

(*data[2]) - "

(*数据[2])——“

(*data[2])[2] - an array of 2 pointers to arrays of two integers.

(*data[2])[2] -一个指向两个整数数组的两个指针的数组的数组。

#5


-1  

If you have an array:

如果你有一个数组:

int myArray[5];
int * myArrayPtr = myArray;

Would be perfectly reasonable. myArray without the brackets is a pointer to an int. When you add the brackets its like you deference the pointer myArray. You could write...

是完全合理的。没有括号的myArray是指向int的指针。当你添加括号时,它就像你对指针myArray的尊重一样。你可以写…

myArrayPtr[1] = 3;

Which is perfectly reasonable. Using parenthesis just makes things harder to read and understand IMO. And they show that people don't understand pointers, arrays, and pointer arithmetic, which is how the compiler or linker goes from one to the other. It seems with parenthesis you do get bounds checking though.

这是完全合理的。使用括号只会使东西更难读懂。它们表明人们不理解指针、数组和指针运算,这是编译器或链接器从一个到另一个的方式。在括号里,你可以检查边界。