Flexible size array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    Flexible size array

    Is the following program conforming under C99?

    #include <stdio.h>

    typedef struct foo {
    int bar;
    int baz[];
    } foo;

    foo foos[]={
    { 1, {1,2,3} }
    };

    int main() {
    return 0;
    }

    gcc -Wall -pedantic -std=c99 (3.3.3 for cygwin) accepts the program
    without the declaration for the array of foo structs, but issues an
    error about "initializa tion of a flexible array member in a nested
    context" on the program above. Is it correct?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Robert Gamble

    #2
    Re: Flexible size array

    Christopher Benson-Manica wrote:[color=blue]
    > Is the following program conforming under C99?
    >
    > #include <stdio.h>
    >
    > typedef struct foo {
    > int bar;
    > int baz[];
    > } foo;
    >
    > foo foos[]={
    > { 1, {1,2,3} }
    > };
    >
    > int main() {
    > return 0;
    > }[/color]

    No, it's not. It is a constraint violation for a structure containing
    a flexible array to be a member of a structure or an array.

    I also don't think that you can initialize the flexible array part in
    the declaration so something like this wouldn't work either:

    foo foo1 = { 1, {1, 2, 3} };

    although this would be okay:

    foo foo1 = {1};
    [color=blue]
    > gcc -Wall -pedantic -std=c99 (3.3.3 for cygwin) accepts the program
    > without the declaration for the array of foo structs, but issues an
    > error about "initializa tion of a flexible array member in a nested
    > context" on the program above. Is it correct?[/color]

    It is correct that there is a problem with your code. I think the
    particular error message you are referring to has to do with the fact
    that that gcc allows static initialization of flexible arrays as well
    as an array of structures containing flexible array member. Because of
    this, things get complicated when you try to statically initialize such
    an array so gcc disallows this.

    Robert Gamble

    Comment

    • akarl

      #3
      Re: Flexible size array

      Robert Gamble wrote:[color=blue]
      > Christopher Benson-Manica wrote:
      >[color=green]
      >>Is the following program conforming under C99?
      >>
      >>#include <stdio.h>
      >>
      >>typedef struct foo {
      >> int bar;
      >> int baz[];
      >>} foo;
      >>
      >>foo foos[]={
      >> { 1, {1,2,3} }
      >>};
      >>
      >>int main() {
      >> return 0;
      >>}[/color]
      >
      >
      > No, it's not. It is a constraint violation for a structure containing
      > a flexible array to be a member of a structure or an array.
      >
      > I also don't think that you can initialize the flexible array part in
      > the declaration so something like this wouldn't work either:
      >
      > foo foo1 = { 1, {1, 2, 3} };
      >
      > although this would be okay:
      >
      > foo foo1 = {1};
      >
      >[color=green]
      >>gcc -Wall -pedantic -std=c99 (3.3.3 for cygwin) accepts the program
      >>without the declaration for the array of foo structs, but issues an
      >>error about "initializa tion of a flexible array member in a nested
      >>context" on the program above. Is it correct?[/color]
      >
      >
      > It is correct that there is a problem with your code. I think the
      > particular error message you are referring to has to do with the fact
      > that that gcc allows static initialization of flexible arrays as well
      > as an array of structures containing flexible array member. Because of
      > this, things get complicated when you try to statically initialize such
      > an array so gcc disallows this.[/color]

      Moreover, according to the standard the signature of `main' should be either

      int main(void)

      or

      int main(int argc, char *argv[])

      (Guess Java makes people forget `void' in C function headers with empty
      parameter lists.)


      August

      Comment

      Working...