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).
For int (*p)[3]: Here “p” is the variable name of the pointer which can point to an array of three integers.
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.
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.
int *a[10] is an array of integer pointers. int (*a)[10] is pointer to an array of integers.
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
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:
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 int
s, and foo[1]
is an array of 3 int
s.
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!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With