Pointers to Arrays

Discussion in 'C Programming' started by kelvSYC, Oct 10, 2003.

  1. kelvSYC

    kelvSYC Guest

    I have a declaration of a pointer to an array of a struct as follows:

    struct foo (*a)[];

    And I have an array of a struct as follows:

    struct foo b[];

    However, a = &b returns an error (illegal assignment to constant).


    Why is this? Is it because the dimensions to b are not defined?

    --
    I am only a mirage.
    kelvSYC, Oct 10, 2003
    #1
    1. Advertising

  2. kelvSYC

    Chris Torek Guest

    In article <091020032212534304%>
    kelvSYC <kelvSYC> writes:
    >I have a declaration of a pointer to an array of a struct as follows:
    >struct foo (*a)[];
    >And I have an array of a struct as follows:
    >struct foo b[];
    >However, a = &b returns an error (illegal assignment to constant).
    >Why is this? Is it because the dimensions to b are not defined?


    If so, that would be a compiler bug. But I suspect the above is
    not *quite* what you have (although "array of unknown size" *is*
    not very useful, hence not used much, hence one should suspect
    compilers might be buggy in this area).

    GCC (under -ansi -pedantic) is perfectly happy with the following,
    which is not proof, but is some evidence that it is OK:

    % cat /tmp/t.c
    struct foo;
    struct foo (*a)[];
    extern struct foo b[];
    void f(void) {
    a = &b;
    }
    % cc -ansi -pedantic -W -Wall -O -c t.c
    %

    The variable "a" has type "pointer to array ? of struct foo", where
    "?" represents "unknown size". (Some prefer to write this as
    "pointer to array of ? `struct foo's".) Note that there is absolutely
    nothing you can do with the variable "a" that you could not do more
    simply with another variable:

    struct foo *a1 = *a;

    Now you can replace any occurrence of (*a) with a1, and any
    occurrence of *a with plain a1. The only thing the pointer in "a"
    would give you that a1 would not is the ability to write a[h]
    for some integer h, but with the size of the arrays to which "a"
    points unspecified, so the only valid value for "h" is an integer
    constant zero: a[0], which is the same as (*a).
    --
    In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://67.40.109.61/torek/index.html (for the moment)
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Oct 10, 2003
    #2
    1. Advertising

  3. "kelvSYC" <> wrote in message
    news:091020032212534304%...
    > I have a declaration of a pointer to an array of a struct as follows:
    >
    > struct foo (*a)[];
    >


    The dimension, here, is not required. It is just a template
    which is required by the compiler to know what /a/ is pointing
    at.

    > And I have an array of a struct as follows:
    >
    > struct foo b[];
    >


    Dimension is required. If you have b already defined somewhere
    then use

    extern struct foo[];

    > However, a = &b returns an error (illegal assignment to constant).
    >
    >
    > Why is this? Is it because the dimensions to b are not defined?
    >
    Vijay Kumar R Zanvar, Oct 10, 2003
    #3
  4. kelvSYC

    James Hu Guest

    On 2003-10-10, kelvSYC <> wrote:
    > I have a declaration of a pointer to an array of a struct as follows:
    > struct foo (*a)[];
    > And I have an array of a struct as follows:
    > struct foo b[];
    > However, a = &b returns an error (illegal assignment to constant).
    > Why is this? Is it because the dimensions to b are not defined?


    In fact, both the declartions of a and b need to have dimensions, and
    they need to be the same number.

    This is needed so that b is the same type as what a is declared to be
    pointing at.

    -- James
    James Hu, Oct 10, 2003
    #4
  5. On Fri, 10 Oct 2003 00:36:42 -0500, James Hu <>
    wrote:

    > On 2003-10-10, kelvSYC <> wrote:
    > > I have a declaration of a pointer to an array of a struct as follows:
    > > struct foo (*a)[];
    > > And I have an array of a struct as follows:
    > > struct foo b[];
    > > However, a = &b returns an error (illegal assignment to constant).
    > > Why is this? Is it because the dimensions to b are not defined?

    >
    > In fact, both the declartions of a and b need to have dimensions, and
    > they need to be the same number.
    >

    No they don't. If at file scope, the declaration of b is actually a
    tentative definition; if not overridden, it allocates an array of one
    element, initialized to appropriate zeros. But even if you add
    'extern' to prevent this, array of unknown bound is compatible with
    any fixed bound, and so AFAICT pointers to same can safely be assigned
    or equivalent. In C99 this is also true at compile time for any VLA
    bound, although it is UB if the actual runtime bounds disagree.

    > This is needed so that b is the same type as what a is declared to be
    > pointing at.
    >

    It appears to technically be a violation (UB) to *access* through a
    pointer to array of the wrong bound, although in practice it will
    almost certainly work as long as any element(s) actually accessed
    is(are) within the actual array object. But if you convert (cast, or
    assign or equivalent) back to pointer to correct bound it must work.

    You won't be able to do arithmetic on, or (thus) subscript, the
    pointer to unknown bound, of course.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Oct 20, 2003
    #5
  6. kelvSYC

    James Hu Guest

    On 2003-10-20, Dave Thompson <> wrote:
    > On Fri, 10 Oct 2003 00:36:42 -0500, James Hu <>
    > wrote:
    >
    >> In fact, both the declartions of a and b need to have dimensions, and
    >> they need to be the same number.
    >>

    > No they don't. [...]
    >


    Thank you. I was actually straightened out after reading Chris Torek's
    post.

    -- James
    James Hu, Oct 20, 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. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    371
  2. Replies:
    1
    Views:
    633
    -berlin.de
    Mar 28, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,104
    Philipp
    Jan 20, 2009
  4. Francesco
    Replies:
    2
    Views:
    1,083
    Francesco
    Nov 6, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    653
Loading...

Share This Page