Kemal Ozan said:
Hi,
I am studying K&R book. On the multidimensional Arrays chapter they
say
"int (*daytab)[13]
is a pointer to an array of 13 integers. The parenthesis are necessary
since brackets [] have higher precedence than *. Without parenthesis,
the declaration
int *daytab[13]
is an array of 13 pointers to integers."
Here is how I get confused: Think about "int *daytab[13]". Since []
have higher precedence than *, I should parenthesize it as "int
*(daytab[])". Now that should mean "a pointer to an array of
integers". Or st. like that... Men, I am already lost.
also int (*daytab)[13] seems to me "an 13 element array of pointers"
if I read according to parenthesis.
totally wrong.
I think I dont know how to interpret/read [] in the first place. int
daytab[13] is an array of 13 integers. That is ok. Better throw out
yourself from window instead of trying to understand when there are
some *s around though.
Can sb. help me about this?
Thanks in advance.
[] = denotes "array of"
* = denotes "pointer to"
[] has higher precedence over *
and parens() have higher precedence over [] and *
So when you encounter () in a declaration, you first read
everything enclosed within the parens starting with the most
inner parens.
Also, we always start with an identifier first. e.g., char hello; hello is
the identifier.
When we encounter parens, *, or [] -- we state what they represent over any
listed types.
That is, if we have ``char bar[10];'' We first state the identifier ``bar''
We then proceed to state what ``[]'' means, which is ``array-of'', followed
by what is
enclosed within the subscript([]) and laslty the type, which is ``char''
We get: bar is array-of 10 char
So let's do this again..
Given the example declaration:
int foo[10];
We first start with the identifier. The identifier here is ``foo''
So we say ``foo''
We then see that ``foo'' is followed by the subscript operator
and that the subscript operator is not empty, so we also
state what is enclosed within the subscript operator
We then get: ``foo is array-of 10''
Lastly, we see the type(int) listed on the left.
We finish this up by stating that type with what we already have:
``foo is array-of 10 int''
So if you follow these same rules in reading C declarations, you should be
able to understand
the declarations which you listed.
int (*daytab)[13];
We see it includes parens, so we start within the parens.
Which gives us: ``* daytab''
``daytab'' is our identifier. So we say the identifiers name..
``daytab''; we now see that the indirection operator is also included within
the parens.
This means that we state what the indirection operator represents
Which is ``pointer to''
We now have: ``daytab is a pointer to''
We are now finished reading what is contained within the parens, so we
discard what
was in those parens and now concentrate on what is outside those parens.
We see the following: [13] and int
Looking back at our rules, we know to say what the subscript operator
represents over
the listed type.
Which now gives us: ``array-of 13'' and then followed by the type of ``int''
Once we construct it all together the declaration now reads: ``daytab is a
pointer to array of 13 int''
Now an additional note, for being able to understand pointers to functions:
parens which designate types of parameters denote functions.
(they could even be empty, but that is considered obsolescent)
e.g.,
int (*foo)(int);
You might be confused here by which to read first, since we have two things
listed within two separate parens. But if you recall, I said start with the
identifier first.
This means, we can just focus on the following of that declaration:
(*foo) and set ``int and (int)'' aside for later.
Given (*foo), this means that ``foo'' is a ``pointer to'', but a pointer to
what?
If we look at the stuff we set aside a moment ago, we can see we have:
int and (int)
What the latter describes is the type of a parameter for a function
We state this as: ``function'' and alternatively we can list the types
enclosed within those parens.
We are now up to ``foo is pointer to function'', and what does this function
return? Well, what else did we set aside from earlier? Oh, the type. Which
was ``int'' (which is the type which is returned)
Finally we can read it as: ``foo is pointer to function returning int''
Or alternatively, if we wish to state the types listed in the function
parens:
``foo is pointer to function (accepting an int) returning int''