`​A selection of topics on IT and its application to finance. ​Send me your comments, questions ​or suggestions by clicking h​ere`

`elmama`
`Decoding complex declarations in C`

​​Complex declarations in C can be very confusing but if you follow this simple rule you need never be confused again. The rule is called the Right-Left rule and is easy to follow. Simply isolate the identifier then look subsequently to its right then to its left until all the terms are used up and build up its declaration from what you find. As always any terms in round brackets need to be worked out first, but still by using the right-left rule. Multiple arrays adjacent to each other are treated as one term (see examples 3 and 4).

A few examples should help clarify the rule.

1) int * (* fred[15]) ()

Applying the rule, the identifier is fred so fred is a "something"
Look to its right and its an array, so fred is an array of "something"
Now look left and its a pointer, so fred is an array of pointers to "something"
Look to the right again and we see a function, so fred  is an array of pointers to a function that returns "something"
Look to the left again and we see another pointer, so  fred is an array of pointers to a function that returns a pointer to "something"

There's nothing more to the right so we look left again and we see an int,  so fred is an array of pointers to a function that returns a pointer to integers.

There are no more terms remaining so that is our final declaration.

2) int *(* fred ())[15]

Applying the rule, the identifier is fred so fred is a "something"
Look to its right and its a function, so fred is a function that returns "something"
Now look left and its a pointer, so fred is a function that returns a pointer to
"something"
Look to the right again and we see an array, so fred is a function that returns a pointer to an array of "something"
Look to the left again and we see another pointer, so fred is a function that returns a pointer to an array of pointers to "something"

There's nothing more to the right so we look left again and we see an int,  so fred is a function that returns a pointer to an array of pointers to integers.

There are no more terms remaining so that is our final declaration.

3) int *fred[4][3]

Applying the rule, the identifier is fred so fred is a "something"
Look to its right and its an 4x3 array, so fred is a 4x3 array of "something"
Now look left and its a pointer, so fred is a 4x3 array of pointers to "something"

There's nothing more on the right so look to the left again and we see an int, so fred is an 4x3 array of pointers to integers

There are no more terms remaining so that is our final declaration i.e space is allocated for 12 pointers. Compare this to the example below where space is allocated for one pointer only.

4) int (*fred)[4][3]

Applying the rule, the identifier is fred so fred is a "something"
We must evaluate bracketed terms first and in this case there nothing immediately on the right of the identifier so look to its left and we see a pointer, so fred is pointer to "something"
Now look to its right and its an 4x3 array, so fred is a pointer to a 4x3 array of "something"
Now look left and we see an int, so fred is a pointer to a 4x3 array of integers.

There are no more terms remaining so that is our final declaration i.e
space is allocated for one pointer.