Specifying array types.

Discussion in 'C++' started by jason.cipriani@gmail.com, Feb 25, 2008.

  1. Guest

    Let's say I have a 2D array like this:

    const double data[100][2] = { ... };

    If I want to assign that array to another pointer variable, I can do
    this:

    typedef double PT[2];
    const PT *dataptr = data;

    How would I declare "dataptr" there without using that typedef? I
    can't figure out the syntax, I've been trying all kinds of weird
    things with no luck. There's no reason why I have to not use a
    typedef, mostly I'm just curious (i.e. not looking for alternatives).

    Thanks,
    Jason
     
    , Feb 25, 2008
    #1
    1. Advertising

  2. Guest

    On Feb 25, 2:48 pm, ""
    <> wrote:
    > Let's say I have a 2D array like this:
    >
    > const double data[100][2] = { ... };


    The first dimension (the "100") is arbitrary, I want dataptr to be
    able to point to any [][2] sized array. Sorry I forgot to say that.

    Jason
     
    , Feb 25, 2008
    #2
    1. Advertising

  3. wrote:
    > Let's say I have a 2D array like this:
    >
    > const double data[100][2] = { ... };
    >
    > If I want to assign that array to another pointer variable, I can do
    > this:
    >
    > typedef double PT[2];
    > const PT *dataptr = data;
    >
    > How would I declare "dataptr" there without using that typedef? I
    > can't figure out the syntax, I've been trying all kinds of weird
    > things with no luck. There's no reason why I have to not use a
    > typedef, mostly I'm just curious (i.e. not looking for alternatives).
    > ...


    const double (*dataptr)[2] = data;

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 25, 2008
    #3
  4. Guest

    On Feb 25, 3:00 pm, Andrey Tarasevich <>
    wrote:
    > const double (*dataptr)[2] = data;


    Thanks. Is that just a matter of memorizing things, or is there some
    rule that you can use to figure out how to declare types like that?
    Function pointers are kind of similar... they're really weird looking.
    I remember it took me a long time to memorize how to declare function
    pointer types, and in the end it was just a memorized syntax, I can't
    get my head around why the parentheses are there and why the variable
    name is in the middle of all that stuff... like, I can't intuitively
    parse that. The first type I tried was this:

    const double[2] * dataptr = data;

    The logic was: if "X *" can be a pointer to an array of X then
    "double[2] *" is a pointer to an array of double[2]'s. That makes
    sense in my head but isn't the right syntax. On the other hand, "const
    double (* dataptr)[2]" doesn't make sense to me when I look at it. I
    guess this is a pretty vague question, but is there some rule there,
    or some method to the madness?

    Thanks,
    Jason
     
    , Feb 25, 2008
    #4
  5. wrote:
    > On Feb 25, 3:00 pm, Andrey Tarasevich <>
    > wrote:
    >> const double (*dataptr)[2] = data;

    >
    > Thanks. Is that just a matter of memorizing things, or is there some
    > rule that you can use to figure out how to declare types like that?


    I always thought there was something in the FAQ. If there isn't, there
    were many attempts to describe the reading of declarations that you
    should be able to find in archives. Just google for "how to read C++
    declarations"

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 25, 2008
    #5
  6. Andrey Tarasevich, Feb 25, 2008
    #6
  7. Guest

    On Feb 25, 3:31 pm, Andrey Tarasevich <>
    wrote:
    > Well, it is often called "right-left-inside-out" rule or something like that
    >
    > http://www.ericgiguere.com/articles/reading-c-declarations.html?nopri...


    Great link; thanks!

    On Feb 25, 3:30 pm, "Victor Bazarov" <> wrote:
    > I always thought there was something in the FAQ.


    I had thought I remembered seeing something like it too once; but I
    didn't have any luck finding it again. Still, the other article has a
    lot of really good info in it.

    Thanks again,
    Jason
     
    , Feb 25, 2008
    #7
  8. Old Wolf Guest

    On Feb 26, 9:09 am, ""
    <> wrote:
    > On Feb 25, 3:00 pm, Andrey Tarasevich <>
    > wrote:
    >
    > > const double (*dataptr)[2] = data;

    >
    > Thanks. Is that just a matter of memorizing things, or is there some
    > rule that you can use to figure out how to declare types like that?


    if you have some declaration declaring x as type T,
    then you can make substitutions:
    - changing x to (*y) means y is a pointer to T
    - changing x to y[N] means y is an array(N) of T
    - changing x to y(A) means y is a function taking
    parameters A and returning T

    Note, N could be blank for an incomplete array type,
    and A is a (possibly empty) list of types.

    For example, if you aren't sure how to make x be a
    pointer to an array[N]; first write the syntax for y
    being an array[N], and then replace y with (*x) .

    Another example: char (*d[24])(void);
    you could build this up:
    char a;
    char b(void);
    char (*c)(void);
    char (*d[24])(void);

    and the meaning is :
    1. a has type: char
    2. b has type: function taking void returning typeof(a)
    3. c has type: pointer to typeof(b)
    4. d has type: array[24] of typeof(c)

    so d is an array[24] of pointers to function taking void returning
    char.
     
    Old Wolf, Feb 27, 2008
    #8
  9. James Kanze Guest

    On Feb 27, 2:21 am, Old Wolf <> wrote:
    > On Feb 26, 9:09 am, ""


    > if you have some declaration declaring x as type T,
    > then you can make substitutions:
    > - changing x to (*y) means y is a pointer to T

    - changing x to *y means y is a pointer to T.
    > - changing x to y[N] means y is an array(N) of T
    > - changing x to y(A) means y is a function taking
    > parameters A and returning T


    Beyond that, you need to know the precedence and the
    associativity: basically, the operators to the right have
    precedence over those to the left, and associativity is always
    with the nearest operators being handled first: left
    associativity for those to the right, and right associativity
    for those to the left. Finally, you can add parentheses to
    change the precedence, or just because you feel like it (or to
    make the precedence clearer).

    For declaring a pointer to an array, as here, you need the
    parentheses because the array "operator" is to the right, and
    thus has precedence:
    int * a [10] ;
    is an array of pointers, i.e.:
    int * (a[10]) ;
    and not:
    int (*a) [10]

    The real fun, of course, starts when you start getting functions
    (and pointers to functions) which return a type requiring one of
    the operators to the right. It's illegal for a function to
    return an array, but it can return a pointer to one:

    int (*f( params ))[ 10 ]
    {
    }

    I'd definitely recommend a few typedef's in such cases.

    > Note, N could be blank for an incomplete array type,
    > and A is a (possibly empty) list of types.


    > For example, if you aren't sure how to make x be a
    > pointer to an array[N]; first write the syntax for y
    > being an array[N], and then replace y with (*x) .


    I think this will always work, because as long as you have the
    pointer operators in parentheses, you'll never need other
    parentheses. But it will result in more parentheses than needed
    in some of the more common cases, e.g. an array of pointers will
    be: first make a y which is a pointer -- (*y) -- then replace
    the y with a an x which is an array -- (*x[10]). You'll end up
    with:
    int (*x[10]) ;
    and not many people would normally use the parentheses there.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 27, 2008
    #9
  10. Old Wolf Guest

    On Feb 27, 10:05 pm, James Kanze <> wrote:
    > On Feb 27, 2:21 am, Old Wolf <> wrote:
    > > if you have some declaration declaring x as type T,
    > > then you can make substitutions:
    > > - changing x to (*y) means y is a pointer to T

    >
    > - changing x to *y means y is a pointer to T.


    Substituting (*y) for x, always makes y a pointer to T.

    Substituting *y could change the meaning of the declaration
    in other ways, e.g.:
    int x[10]; // array[10] of int
    int (*y)[10]; // pointer to array[10] of int
    int *y[10]; // not pointer to array[10] of int

    Hence why I do not advocate making that substitution.

    > Beyond that, you need to know the precedence and the
    > associativity


    I don't see why. Can you give an example of a
    declaration that can't be solved using my method?

    > int * a [10] ;


    This is int *b; and the b substituted out for a[10],
    so a is an array[10] of what b was, i.e. pointer to int.
     
    Old Wolf, Feb 28, 2008
    #10
  11. On Feb 25, 8:09 pm, ""
    <> wrote:
    > On Feb 25, 3:00 pm, Andrey Tarasevich <>
    > wrote:
    >
    > > const double (*dataptr)[2] = data;

    >
    > Thanks. Is that just a matter of memorizing things, or is there some
    > rule that you can use to figure out how to declare types like that?


    In addition to what others have said, I'd add that this is
    one of those things that looks very complicated when spelled
    out in an algorithm, but is a lot easier in practice - sort
    of like riding a bicycle. You could informally summarise reading
    a decaration as "start in the middle, start going right, and then
    ricochet between parens". At least this is how I mentally model
    it when I'm doing it !
     
    tragomaskhalos, Feb 28, 2008
    #11
    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. Sathyaish
    Replies:
    2
    Views:
    592
    Sathyaish
    May 22, 2005
  2. No One

    Specifying data types inside functions

    No One, Apr 10, 2004, in forum: C Programming
    Replies:
    4
    Views:
    357
    Munawar Akram
    Apr 10, 2004
  3. Replies:
    4
    Views:
    1,407
  4. Jeff 'Jones' Putz

    Specifying string array as an attribute in HTML tag

    Jeff 'Jones' Putz, Jul 28, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    145
    Jeff 'Jones' Putz
    Jul 28, 2003
  5. Tuxedo
    Replies:
    4
    Views:
    101
    Tuxedo
    Sep 1, 2008
Loading...

Share This Page