When working with arrays and pointers in C, one quickly discovers that they are by no means equivalent although it might seem so at a first glance. I know about the differen
Its probably easier for the compiler to interpret than it is to you. A language defines a set of rules for what constitutes a valid expression and the compiler uses these rules to interpret such expressions (might sound complicated but compilers have been studied in length and there are a lot of standardized algorithms and tools available, you might want to read about parsing). cdecl is a tool that will translate C expressions to English. The source code is available on the website so you can check it out. The book The C++ Programming Language included sample code for writing such a program if I'm not mistaking.
As for interpreting these expressions yourself, I found the best technique to do that in the book Thinking in C++ volume 1:
To define a pointer to a function that has no arguments and no return value, you say:
void (*funcPtr)();
When you are looking at a complex definition like this, the best way to attack it is to start in the middle and work your way out. “Starting in the middle” means starting at the variable name, which is funcPtr. “Working your way out” means looking to the right for the nearest item (nothing in this case; the right parenthesis stops you short), then looking to the left (a pointer denoted by the asterisk), then looking to the right (an empty argument list indicating a function that takes no arguments), then looking to the left (void, which indicates the function has no return value). This right-left-right motion works with most declarations.
To review, “start in the middle” (“funcPtr is a ...”), go to the right (nothing there – you're stopped by the right parenthesis), go to the left and find the ‘*’ (“... pointer to a ...”), go to the right and find the empty argument list (“... function that takes no arguments ... ”), go to the left and find the void (“funcPtr is a pointer to a function that takes no arguments and returns void”).
You can download the book for free on Bruce Eckel's download site. Let's try to apply it to int (*a)[3]
:
In situations when you don't know what is declaration for, I found very helpful so called Right-left rule.
BTW. Once you've learned it, such declarations are peace of cake:)
You may find this article helpful:
I've made this answer a community wiki (since it's just a link to an article, not an answer), if anyone wants to add further resources to it.
It means
declare a as pointer to array 3 of int
See cdecl for future reference.
Why what is "not int *(a[3])
" exactly (referring to your last question)? The int *(a[3])
is the same as plain int *a[3]
. The braces are redundant. It is an array of 3 pointers to int
and you said you know what it means.
The int (*a)[3]
is a pointer to an array of 3 int
(i.e. a pointer to int[3]
type). The braces in this case are important. You yourself provided a correct example the makes an equivalent declaration through an intermediate typedef
.
The simple popular inside-out C declaration reading rule works in this case perfectly well. In the int *a[3]
declaration we'd have to start from a
and go right first, i.e. to get a[3]
, meaning that a
is an array of 3 something (and so on). The ()
in int (*a)[3]
changes that, so we can't go right and have to start from *a
instead: a
is a pointer to something. Continuing to decode the declaration we'll come to the conclusion that that something is an array of 3 int
.
In any case, find a good tutorial on reading C declarations, of which there are quite a few available on the Net.
Every time you have doubts with complex declarations, you can use the cdecl tool in Unix like systems:
[/tmp]$ cdecl
Type `help' or `?' for help
cdecl> explain int (*a)[10];
declare a as pointer to array 10 of int
EDIT:
There is also a online version of this tool available here.
Thanks to Tiberiu Ana and gf