C99 Standard, arrays of size omitted

Discussion in 'C Programming' started by Peteris Krumins, Jun 27, 2003.

  1. Hello,

    C99 Standard states that the last member of structure can be an array
    whose size is omitted. What's the point of having such member in a
    structure and how can it be effectivly used?

    struct foo {
    int value;
    long another;
    char arr[0];
    };

    Also can i simply have a variable (array) which has size omitted?

    int omitted[0];
    char *pointers[0]; /* is this valid too? */

    P.Krumins
     
    Peteris Krumins, Jun 27, 2003
    #1
    1. Advertising

  2. Peteris Krumins

    Simon Biber Guest

    "Peteris Krumins" <> wrote:
    > Hello,
    >
    > C99 Standard states that the last member of structure can be
    > an array whose size is omitted. What's the point of having
    > such member in a structure and how can it be effectivly
    > used?
    >
    > struct foo {
    > int value;
    > long another;
    > char arr[0];
    > };


    Actually, you didn't omit the size, you made it zero, which is
    wrong.

    The proper C99 syntax is:
    struct foo {
    int value;
    long another;
    char arr[];
    };

    The way you use it is usually with malloc:
    struct foo *p = malloc(n + sizeof *p);
    if(p != NULL)
    {
    /* Now p->arr has n elements available. */
    /* ... */
    free(p);
    }

    This was added because of the *wrong* existing practise to do:
    struct foo {
    int value;
    long another;
    char arr[1]; /* Note one element */
    };
    struct foo *p = malloc(n + sizeof *p - 1);
    if(p != NULL)
    {
    /* Now p->arr is used as if it has n elements available. */
    /* ... */
    free(p);
    }
    This is wrong because it is still undefined behaviour to
    access an element outside the declared array size of 1.
    It does work on most C89 compilers.

    > Also can i simply have a variable (array) which has size
    > omitted?


    Only in a couple of cases. The first dimension of a function
    parameter declared using array syntax can have its size
    omitted. This is OK because the array size is never used --
    the parameter's type is always rewritten into a pointer to
    the first element.

    For example:
    int foo(int arr[][5]);
    is rewritten as:
    int foo(int (*arr)[5]);

    Another example is where you're making a declaration of an
    array which is defined in another translation unit:
    extern int arr[];

    > int omitted[0];
    > char *pointers[0]; /* is this valid too? */


    Neither of these are valid. Omitting the size does *not*
    mean you can make it zero.

    --
    Simon.
     
    Simon Biber, Jun 28, 2003
    #2
    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. Newgene
    Replies:
    1
    Views:
    539
    Peter Otten
    Dec 2, 2004
  2. Replies:
    3
    Views:
    3,683
    Chris Torek
    Feb 20, 2006
  3. Replies:
    8
    Views:
    782
    eminemence
    Jan 25, 2006
  4. Replies:
    3
    Views:
    603
    Keith Thompson
    Mar 31, 2007
  5. Daniel Pitts
    Replies:
    0
    Views:
    360
    Daniel Pitts
    May 22, 2007
Loading...

Share This Page