Can someone explain what this means?
int (*data[2])[2];
Parentheses are used in C expressions in the same manner as in algebraic expressions.
Parenthesis definition Parenthesis refer to punctuation marks "(" and ")" used to separate relevant information or a comment from the rest of the text, or to enclose mathematical symbols, or the text inside of these marks. The punctuation marks in the math equation 2x(4+6) are an example of parenthesis. noun.
Valid Parentheses in C++ The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid. The task is simple; we will use the stack to do this.
What are the parentheses for?
In C brackets [] have a higher precedence than the asterisk *
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:
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.
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.
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]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With