Interpretation of int (*a)[3]

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


It declares a pointer to an array of 3 ints.

The parentheses are necessary as the following declares an array of 3 pointers to int:

int* a[3];

You get better readability when using typedef:

typedef int threeInts[3];
threeInts* pointerToThreeInts;

First, you mean "typedef" not "typecast" in your question.

In C, a pointer to type T can point to an object of type T:

int *pi;
int i;
pi = &i;

The above is simple to understand. Now, let's make it a bit more complex. You seem to know the difference between arrays and pointers (i.e., you know that arrays are not pointers, they behave like them sometimes though). So, you should be able to understand:

int a[3];
int *pa = a;

But for completeness' sake: in the assignment, the name a is equivalent to &a[0], i.e., a pointer to the first element of the array a. If you are not sure about how and why this works, there are many answers explaining exactly when the name of an array "decays" to a pointer and when it does not:

  • My answer to a question titled type of an array,
  • Another answer with examples of instances when the name of an array does not decay to a pointer, and
  • The answers to what is array decaying.

I am sure there are many more such questions and answers on SO, I just mentioned some that I found from a search.

Back to the topic: when we have:

int foo[2][3];

foo is of type "array [2] of array [3] of int". This means that foo[0] is an array of 3 ints, and foo[1] is an array of 3 ints.

Now let's say we want to declare a pointer, and we want to assign that to foo[0]. That is, we want to do:

/* declare p somehow */
p = foo[0];

The above is no different in form to the int *pa = a; line, because the types of a and of foo[0] are the same. So, we need int *p; as our declaration of p.

Now, the main thing to remember about arrays is that "the rule" about array's name decaying to a pointer to its first element applies only once. If you have an array of an array, then in value contexts, the name of the array will not decay to the type "pointer to pointer", but rather to "pointer to array". Going back to foo:

/* What should be the type of q? */
q = foo;

The name foo above is a pointer to the first element of foo, i.e., we can write the above as:

q = &foo[0];

The type of foo[0] is "array [3] of int". So we need q to be a pointer to an "array [3] of int":

int (*q)[3];

The parentheses around q are needed because [] binds more tightly than * in C, so int *q[3] declares q as an array of pointers, and we want a pointer to an array. int *(q[3]) is, from above, equivalent to int *q[3], i.e., an array of 3 pointers to int.

Hope that helps. You should also read C for smarties: arrays and pointers for a really good tutorial on this topic.

About reading declarations in general: you read them "inside-out", starting with the name of the "variable" (if there is one). You go left as much as possible unless there is a [] to the immediate right, and you always honor parentheses. cdecl should be able to help you to an extent:

$ cdecl
cdecl> declare p as  pointer to array 3 of int
int (*p)[3]
cdecl> explain int (*p)[3]
declare p as pointer to array 3 of int

To read

int (*a)[3];

      a            # "a is"
    (* )           # parentheses, so precedence changes.
                   # "a pointer to"
        [3]        # "an array [3] of"
int        ;       # "int".

For

int *a[3];

     a             # "a is"
      [3]          # "an array [3] of"
    *              # can't go right, so go left.
                   # "pointer to"
int      ;         # "int".

For

char *(*(*a[])())()

          a         # "a is"
           []       # "an array of"
         *          # "pointer to"
        (    )()    # "function taking unspecified number of parameters"
      (*        )   # "and returning a pointer to"
                 () # "function"
char *              # "returning pointer to char"

(Example from c-faq question 1.21. In practice, if you are reading such a complicated declaration, there is something seriously wrong with the code!)