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

Discussion in 'C Programming' started by Kemal Ozan, Oct 6, 2003.

  1. Kemal Ozan

    Kemal Ozan Guest

    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.
     
    Kemal Ozan, Oct 6, 2003
    #1
    1. Advertising

  2. Kemal Ozan

    Kris Wempa Guest

    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" <> wrote in message
    news:eLdgb.7215$...
    >
    > "Kemal Ozan" <> wrote in message
    > news:...
    > > 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''
    >
    >
     
    Kris Wempa, Oct 6, 2003
    #2
    1. Advertising

  3. Kemal Ozan

    Simon Biber Guest

    "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.
     
    Simon Biber, Oct 6, 2003
    #3
  4. Kemal Ozan

    j Guest

    "Kemal Ozan" <> wrote in message
    news:...
    > 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''
     
    j, Oct 6, 2003
    #4
  5. Kemal Ozan

    CBFalconer Guest

    j wrote:
    > "Kemal Ozan" <> 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 6, 2003
    #5
  6. Kemal Ozan

    Kemal Ozan Guest

    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" <> wrote in message news:<eLdgb.7215$>...
    > "Kemal Ozan" <> wrote in message
    > news:...
    > > 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''
     
    Kemal Ozan, Oct 7, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,227
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,657
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    460
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    962
    Jim Langston
    Oct 20, 2005
  5. magicman
    Replies:
    5
    Views:
    411
    santosh
    May 4, 2008
Loading...

Share This Page