Equivalent Declarations C

Are

int (*x)[10]; 

and

 int x[10]; 

equivalent?

According to the clockwise rule , they analyze different C declarations.

For click fatigue:

"Clockwise / Spiral Rule" by David Anderson

There is a technology known as the `` Clockwise / Spiral Rule '' that allows any C programmer to analyze their head - any C declaration!

There are three simple steps:

  1. Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements: [X] or [] => Array X size of... or Array undefined size of... (type1, type2) => function passing type1 and type2 returning... * => pointer(s) to... 2. Keep doing this in a spiral/clockwise direction until all tokens have been covered. 3. Always resolve anything in parenthesis first! 
+4
source share
5 answers

They are not equal. in the first case, x is a pointer to an array of 10 integers, in the second case, x is an array of 10 integers.

Both types are different. You can see that this is not the same thing by checking sizeof in two cases.

+8
source

Follow this simple process when reading ads:

Start with a variable name (or an internal construct if the identifier is real. Look straight ahead without jumping over the right bracket; say what you see. Look left again without jumping over the bracket; say what you see. Pop out the level of parentheses if Any Look straight; say what you see. Look left; say what you see. Continue this way until you say the variable type or return type.

So:

 int (*x)[10]; 

x is a pointer to an array of 10 int s

 int x[10]; 

x is an array of 10 int s

 int *x[10]; 

x is an array of 10 pointers to int s

+8
source

I tend to follow the Rule of Priority for Understanding C Declarations , which is very well presented in Expert C Programming - Deep C Secrets of Peter van der Linden

 A - Declarations are read by starting with the name and then reading in precedence order. B - The precedence, from high to low, is: B.1 parentheses grouping together parts of a declaration B.2 the postfix operators: parentheses () indicating a function, and square brackets [] indicating an array. B.3 the prefix operator: the asterisk denoting "pointer to". C If a const and/or volatile keyword is next to a type specifier (eg int, long, etc.) it applies to the type specifier. Otherwise the const and/or volatile keyword applies to the pointer asterisk on its immediate left. 
+4
source

It’s easier for me to remember the rule without any explicit groupings, () and [] bind to * . So for type declaration

 T *a[N]; 

[] binds before * , so a is an N-element pointer array. Breaking it step by step:

  a -- a a[N] -- is an N-element array *a[N] -- of pointer T *a[N] -- to T. 

For type declaration

 T (*a)[N]; 

paranes cause * to bind before [] , so

  a -- a (*a) -- is a pointer (*a)[N] -- to an N-element array T (*a)[N] -- of T 

This is still a clockwise / spiral rule, simply expressed more compactly.

+2
source

No. First, an array of 10 int pointers is declared, and the second declares an array of 10 integers.

-4
source

Source: https://habr.com/ru/post/1345260/


All Articles