Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpretation of int (*a)[3]

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 differences in L-values and R-values. Still, recently I tried to find out the type of a pointer that I could use in conjunction with a two-dimensional array, i.e.

int foo[2][3]; int (*a)[3] = foo; 

However, I just can't find out how the compiler "understands" the type definition of a in spite of the regular operator precedence rules for * and []. If instead I were to use a typedef, the problem becomes significantly simpler:

int foo[2][3]; typedef int my_t[3]; my_t *a = foo; 

At the bottom line, can someone answer me the questions as to how the term int (*a)[3] is read by the compiler? int a[3] is simple, int *a[3] is simple as well. But then, why is it not int *(a[3])?

EDIT: Of course, instead of "typecast" I meant "typedef" (it was just a typo).

like image 416
fotNelton Avatar asked Feb 12 '10 08:02

fotNelton


People also ask

What is the purpose of P in the following statement int (* p 3 ]) ();?

For int (*p)[3]: Here “p” is the variable name of the pointer which can point to an array of three integers.

What is the meaning of (* ptr )*= 10?

Effectively, you are creating a pointer to a pointer (which is a pointer to an array) and allocating the first pointer to an array that 10 elements. Therefore, when you run the following lines: d[0] = 7; d[1] = 10; You are assigning the 1st array's address to 7 and the second array's address to 10.

What is the significance of int * p?

int **p declares a pointer on the stack which points to pointer(s) on the heap. Each of that pointer(s) point to an integer or array of integers on the heap.

What is the difference between int * A 10 and int (* A )[ 10 ]?

int *a[10] is an array of integer pointers. int (*a)[10] is pointer to an array of integers.


2 Answers

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

like image 98
codaddict Avatar answered Sep 25 '22 17:09

codaddict


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!)

like image 23
Alok Singhal Avatar answered Sep 22 '22 17:09

Alok Singhal