Velocity Reviews > int (*daytab)[13] vs int *daytab[13]

int (*daytab)[13] vs int *daytab[13]

Kemal Ozan
Guest
Posts: n/a

 10-06-2003
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.

Kris Wempa
Guest
Posts: n/a

 10-06-2003
In practice, you'll almost NEVER see "int (*daytab)[13]". There's just no
need for the extra level of indirection, especially when you can just use
'daytab' as a pointer to the first array element. Other than understanding
the language, I wouldn't worry too much if the "int (*daytab)[13]" doesn't
completely make sense to you.

"j" <(E-Mail Removed)> wrote in message
news:eLdgb.7215\$(E-Mail Removed)...
>
> "Kemal Ozan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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.
> >

>
>
> [] = 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
> 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''
>
>

Simon Biber
Guest
Posts: n/a

 10-06-2003
"Kris Wempa" <calmincents(NO_SPAM)@yahoo.com> wrote:
> In practice, you'll almost NEVER see "int (*daytab)[13]". There's just no
> need for the extra level of indirection, especially when you can just use
> 'daytab' as a pointer to the first array element. Other than understanding
> the language, I wouldn't worry too much if the "int (*daytab)[13]" doesn't
> completely make sense to you.

You make a 'pointer to array' every time you use a multi-dimensional array in C.

int foo(int array[2][3])
{
return array[1][2];
}

This function actually takes an int(*)[3] and the prototype of this function
can be written as:
int foo(int (*array)[3]);

--
Simon.

j
Guest
Posts: n/a

 10-06-2003

"Kemal Ozan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.
>

[] = 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];
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];

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
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''

CBFalconer
Guest
Posts: n/a

 10-06-2003
j wrote:
> "Kemal Ozan" <(E-Mail Removed)> wrote in message
>
> > 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.
> >

.... snip ...
>
> [] = 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.

.... snip excellent tutorial ...

Very nice job.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Kemal Ozan
Guest
Posts: n/a

 10-07-2003
Thanks alot... Miraculously, I seem to be decoding them...
I even am able to word these:
char (*(*x())[])() : x is a function-returning pointer-to array-of
pointers-to functions-returning char

char (*(*x[3])())[5] : x is an array-of 3 pointers-to
functions-returning pointer-to array-of 5 chars.

"j" <(E-Mail Removed)> wrote in message news:<eLdgb.7215\$(E-Mail Removed)>.. .
> "Kemal Ozan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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.
> >

>
>
> [] = 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
> 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''