Array of zero element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nileshsimaria@gmail.com

    Array of zero element

    Hi,

    I have seen some code were we have array with zero elements (mostly
    within structure)

    like,

    typedef struct foo {
    int data[0]
    }foo;

    Just curious whats the use of this kind of code ? And how to use member
    variable 'data' of struct foo ?

    Thanks,
    Nilesh

  • Richard Heathfield

    #2
    Re: Array of zero element

    nileshsimaria@g mail.com said:
    Hi,
    >
    I have seen some code were we have array with zero elements (mostly
    within structure)
    >
    like,
    >
    typedef struct foo {
    int data[0]
    }foo;
    >
    Just curious whats the use of this kind of code ?
    It's good for generating diagnostic messages, such as:

    warning: ANSI C forbids zero-size array `data'
    warning: no semicolon at end of struct or union
    And how to use member
    variable 'data' of struct foo ?
    Make it a big bigger than 0, stick a semicolon on the end, instantiate it,
    and then use the member operator . to access the member object.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at the above domain, - www.

    Comment

    • nileshsimaria@gmail.com

      #3
      Re: Array of zero element


      Richard Heathfield wrote:
      nileshsimaria@g mail.com said:
      >
      Hi,

      I have seen some code were we have array with zero elements (mostly
      within structure)

      like,

      typedef struct foo {
      int data[0]
      }foo;

      Just curious whats the use of this kind of code ?
      >
      It's good for generating diagnostic messages, such as:
      >
      warning: ANSI C forbids zero-size array `data'
      warning: no semicolon at end of struct or union
      Ok, I put semicolon and compiling using gcc, its not producing warning,
      >
      And how to use member
      variable 'data' of struct foo ?
      >
      Make it a big bigger than 0, stick a semicolon on the end, instantiate it,
      and then use the member operator . to access the member object.
      >
      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at the above domain, - www.

      Comment

      • Cong Wang

        #4
        Re: Array of zero element


        nileshsimaria@g mail.com wrote:
        Hi,
        >
        I have seen some code were we have array with zero elements (mostly
        within structure)
        >
        like,
        >
        typedef struct foo {
        int data[0]
        }foo;
        >
        Just curious whats the use of this kind of code ? And how to use member
        variable 'data' of struct foo ?
        >
        Thanks,
        Nilesh
        Array of zero length is NOT standard C, but a GCC extension. In the
        book "GCC - The Complete Reference", it is said that:
        "GNU C allows the declaration of arrays of zero length to facilitate
        the creation of
        variable-length structures. This only makes sense if the zero-length
        array is the last
        member of a struct. The size of the array can be specified by simply
        being allocated
        the amount of space necessary. The following program demonstrates the
        technique:
        /* zarray.c */
        #include <stdio.h>
        typedef struct {
        int size;
        char string[0];
        } vlen;
        int main(int argc,char *argv[])
        {
        int i;
        int count = 22;
        char letter = 'a';
        vlen *line = (vlen *)malloc(sizeof (vlen) + count);
        line->size = count;
        for(i=0; i<count; i++)
        line->string[i] = letter++;
        printf("sizeof( vlen)=%d\n",siz eof(vlen));
        for(i=0; i<line->size; i++)
        printf("%c ",line->string[i]);
        printf("\n");
        return(0);
        }
        "

        Comment

        • Richard Heathfield

          #5
          Re: Array of zero element

          nileshsimaria@g mail.com said:
          >
          Richard Heathfield wrote:
          >nileshsimaria@g mail.com said:
          >>
          Hi,
          >
          I have seen some code were we have array with zero elements (mostly
          within structure)
          >
          like,
          >
          typedef struct foo {
          int data[0]
          }foo;
          >
          Just curious whats the use of this kind of code ?
          >>
          >It's good for generating diagnostic messages, such as:
          >>
          >warning: ANSI C forbids zero-size array `data'
          >warning: no semicolon at end of struct or union
          >
          Ok, I put semicolon and compiling using gcc, its not producing warning,
          Let's put the semicolon in, then, and re-compile using gcc:

          warning: ANSI C forbids zero-size array `data'

          If you're not getting this warning, I suggest you crank up your warning
          level. To compile your code, I used my usual options:

          gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
          -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
          -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
          -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
          -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at the above domain, - www.

          Comment

          • Tom St Denis

            #6
            Re: Array of zero element

            Cong Wang wrote:
            Array of zero length is NOT standard C, but a GCC extension. In the
            book "GCC - The Complete Reference", it is said that:
            "GNU C allows the declaration of arrays of zero length to facilitate
            the creation of
            variable-length structures. This only makes sense if the zero-length
            array is the last
            member of a struct. The size of the array can be specified by simply
            being allocated
            the amount of space necessary. The following program demonstrates the
            technique:
            What a horrible unportable idea.
            int count = 22;
            char letter = 'a';
            vlen *line = (vlen *)malloc(sizeof (vlen) + count);
            line->size = count;
            Why not just make string a "char*" ???

            Tom

            Comment

            • Cong Wang

              #7
              Re: Array of zero element


              Tom St Denis wrote:
              Cong Wang wrote:
              Array of zero length is NOT standard C, but a GCC extension. In the
              book "GCC - The Complete Reference", it is said that:
              "GNU C allows the declaration of arrays of zero length to facilitate
              the creation of
              variable-length structures. This only makes sense if the zero-length
              array is the last
              member of a struct. The size of the array can be specified by simply
              being allocated
              the amount of space necessary. The following program demonstrates the
              technique:
              >
              What a horrible unportable idea.
              Of course I know that's NOT portable. I *just* want to explain what
              zero length array is and how it is used.
              >
              int count = 22;
              char letter = 'a';
              vlen *line = (vlen *)malloc(sizeof (vlen) + count);
              line->size = count;
              >
              Why not just make string a "char*" ???
              >
              Tom
              If I wrote the code, I will use char* too. But the code is picked from
              that book, which, of course, is not written by me.

              Comment

              • Richard Tobin

                #8
                Re: Array of zero element

                In article <1164806203.416 069.220160@80g2 000cwy.googlegr oups.com>,
                Tom St Denis <tomstdenis@gma il.comwrote:
                >"GNU C allows the declaration of arrays of zero length to
                >facilitate the creation of variable-length structures. This only
                >makes sense if the zero-length array is the last member of a
                >struct. The size of the array can be specified by simply being
                >allocated the amount of space necessary.
                >What a horrible unportable idea.
                It's a technique that has been used in many programming languages,
                probably since the 1950s. For example, it was a common way of
                representing Lisp symbols, which had some fixed size parts (e.g. value
                cell) and a variable-length name.

                It's sufficiently common in C that is has a name: the "struct hack",
                and has been incorporated into the language (with a different syntax)
                in C99.
                >Why not just make string a "char*" ???
                Because it requires an extra pointer, an extra indirection, and
                doubles the number of allocations required[*], which may be very
                significant in some applications.
                [*] Actually in this case it would be straightforward to allocate
                enough for the struct plus the string, and set the pointer
                accordingly.

                -- Richard

                --
                "Considerat ion shall be given to the need for as many as 32 characters
                in some alphabets" - X3.4, 1963.

                Comment

                • Jean-Marc Bourguet

                  #9
                  Re: Array of zero element

                  Richard Heathfield <rjh@see.sig.in validwrites:
                  gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
                  -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
                  -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
                  -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                  -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
                  What's the aim of having both -Wconversion and -Wno-conversion?

                  Yours,

                  --
                  Jean-Marc

                  Comment

                  • Richard Heathfield

                    #10
                    Re: Array of zero element

                    Jean-Marc Bourguet said:
                    Richard Heathfield <rjh@see.sig.in validwrites:
                    >
                    >gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
                    >-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
                    >-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
                    >-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                    >-Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
                    >
                    What's the aim of having both -Wconversion and -Wno-conversion?
                    Good spot! How did that get in there? :-)

                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at the above domain, - www.

                    Comment

                    • CBFalconer

                      #11
                      Re: Array of zero element

                      nileshsimaria@g mail.com wrote:
                      >
                      I have seen some code were we have array with zero elements
                      (mostly within structure) like,
                      >
                      typedef struct foo {
                      int data[0]
                      }foo;
                      >
                      Just curious whats the use of this kind of code ? And how to use
                      member variable 'data' of struct foo ?
                      Only in C99 code, and only when data is the last field in the
                      struct foo. It is a means of legitimizing the "struct hack", which
                      you can google for. Alternatively, read the standard.

                      --
                      Chuck F (cbfalconer at maineline dot net)
                      Available for consulting/temporary embedded and systems.
                      <http://cbfalconer.home .att.net>


                      Comment

                      • CBFalconer

                        #12
                        Re: Array of zero element

                        nileshsimaria@g mail.com wrote:
                        Richard Heathfield wrote:
                        >nileshsimaria@g mail.com said:
                        >>
                        >>I have seen some code were we have array with zero elements
                        >>(mostly within structure) like,
                        >>>
                        >>typedef struct foo {
                        >> int data[0]
                        >>}foo;
                        >>>
                        >>Just curious whats the use of this kind of code ?
                        >>
                        >It's good for generating diagnostic messages, such as:
                        >>
                        >warning: ANSI C forbids zero-size array `data'
                        >warning: no semicolon at end of struct or union
                        >
                        Ok, I put semicolon and compiling using gcc, its not producing warning,
                        By default gcc is not a C compiler. You need at least

                        -W -Wall -ansi -pedantic

                        switches to make it one. Besides, there is no requirement for any
                        compiler to produce any particular warning.

                        --
                        Chuck F (cbfalconer at maineline dot net)
                        Available for consulting/temporary embedded and systems.
                        <http://cbfalconer.home .att.net>


                        Comment

                        • Ben Pfaff

                          #13
                          Re: Array of zero element

                          CBFalconer <cbfalconer@yah oo.comwrites:
                          nileshsimaria@g mail.com wrote:
                          >>
                          >I have seen some code were we have array with zero elements
                          >(mostly within structure) like,
                          >>
                          >typedef struct foo {
                          > int data[0]
                          >}foo;
                          >>
                          >Just curious whats the use of this kind of code ? And how to use
                          >member variable 'data' of struct foo ?
                          >
                          Only in C99 code, and only when data is the last field in the
                          struct foo. It is a means of legitimizing the "struct hack", which
                          you can google for. Alternatively, read the standard.
                          C99 uses [] instead of [0] for its legitimate version of the
                          struct hack, which is by the way called a "flexible array
                          member".
                          --
                          "The expression isn't unclear *at all* and only an expert could actually
                          have doubts about it"
                          --Dan Pop

                          Comment

                          • Keith Thompson

                            #14
                            Re: Array of zero element

                            nileshsimaria@g mail.com writes:
                            I have seen some code were we have array with zero elements (mostly
                            within structure)
                            >
                            like,
                            >
                            typedef struct foo {
                            int data[0]
                            }foo;
                            >
                            Just curious whats the use of this kind of code ? And how to use member
                            variable 'data' of struct foo ?
                            It's a form of the "struct hack". It's explained in question 2.6 of
                            the comp.lang.c FAQ, <http://www.c-faq.com/>. A more portable form
                            uses "int data[1];", but even that is of questionable legality. C99
                            replaces the struct hack with flexible array members, but not all
                            compilers yet support that feature.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • Peter Nilsson

                              #15
                              Re: Array of zero element

                              CBFalconer wrote:
                              nileshsimaria@g mail.com wrote:
                              Ok, I put semicolon and compiling using gcc, its not producing warning,
                              >
                              By default gcc is not a C compiler. You need at least
                              >
                              -W -Wall -ansi -pedantic
                              >
                              switches to make it one. Besides,
                              Did you mean 'otherwise'?
                              there is no requirement for any
                              compiler to produce any particular warning.
                              5.1.1.3p1:

                              "A conforming implementation shall produce at least one diagnostic
                              message
                              (identified in an implementation-defined manner) if a preprocessing
                              translation
                              unit or translation unit contains a violation of any syntax rule or
                              constraint,
                              even if the behavior is also explicitly specified as undefined or
                              implementation-
                              defined. ..."

                              --
                              Peter

                              Comment

                              Working...