struct question: NULL terminated char* array

Discussion in 'C Programming' started by chy1013m1@gmail.com, Sep 4, 2006.

  1. Guest

    char *testCharArray[] = {
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    NULL,
    };

    The above code initializes a NULL terminated char* array. The question
    is why is a semi-colon needed after the bracket (ie. };), and why
    should there be a comma after NULL ? (I saw in the book beginning linux
    programming 3rd ed. the auther appended a comma after NULL, removing
    the comma does nothing to the code)


    compiler I used was gcc 3.4.4
     
    , Sep 4, 2006
    #1
    1. Advertising

  2. wrote:
    > char *testCharArray[] = {
    > "one",
    > "two",
    > "three",
    > "four",
    > "five",
    > "six",
    > NULL,
    > };
    >
    > The above code initializes a NULL terminated char* array. The question
    > is why is a semi-colon needed after the bracket (ie. };)


    Because declarations in C end with a semicolon, the closing bracket
    signifies the end of the initializer list, not the end of the
    declaration.

    > and why should there be a comma after NULL ? (I saw in the book beginning
    > linux programming 3rd ed. the auther appended a comma after NULL,
    > removing the comma does nothing to the code)


    Trailing commas at the end of an initializer list are a feature
    intended to aid automatic code generators, they are not required.

    Robert Gamble
     
    Robert Gamble, Sep 4, 2006
    #2
    1. Advertising

  3. CBFalconer Guest

    Robert Gamble wrote:
    > wrote:
    >
    >> char *testCharArray[] = {
    >> "one",
    >> "two",
    >> "three",
    >> "four",
    >> "five",
    >> "six",
    >> NULL,
    >> };
    >>

    .... snip ...
    >
    >> and why should there be a comma after NULL ? (I saw in the book
    >> beginning linux programming 3rd ed. the auther appended a comma
    >> after NULL, removing the comma does nothing to the code)

    >
    > Trailing commas at the end of an initializer list are a feature
    > intended to aid automatic code generators, they are not required.


    IMO a better way to write that declaration (and be compatible with
    all C versions) is:

    char *testCharArray[] = { "one"
    , "two"
    , "three"
    , "four"
    , "five"
    , "six"
    , NULL
    };

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
     
    CBFalconer, Sep 4, 2006
    #3
  4. Old Wolf Guest

    Robert Gamble wrote:
    > wrote:
    > > char *testCharArray[] = {
    > > "one",
    > > "two",
    > > "three",
    > > "four",
    > > "five",
    > > "six",
    > > NULL,
    > > };

    >
    > > and why should there be a comma after NULL ?

    >
    > Trailing commas at the end of an initializer list are a feature
    > intended to aid automatic code generators, they are not required.


    To be clearer, this trailing comma means the program is
    not a well-formed C90 program -- although it is a very common
    compiler extension to allow it.
     
    Old Wolf, Sep 5, 2006
    #4
  5. Ben Pfaff Guest

    "Old Wolf" <> writes:

    > Robert Gamble wrote:
    >> wrote:
    >> > char *testCharArray[] = {
    >> > "one",
    >> > "two",
    >> > "three",
    >> > "four",
    >> > "five",
    >> > "six",
    >> > NULL,
    >> > };

    >>
    >> > and why should there be a comma after NULL ?

    >>
    >> Trailing commas at the end of an initializer list are a feature
    >> intended to aid automatic code generators, they are not required.

    >
    > To be clearer, this trailing comma means the program is
    > not a well-formed C90 program -- although it is a very common
    > compiler extension to allow it.


    No, you're wrong about that. C90 and C99 accept trailing commas
    in all the same places, except for one: only C99 accepts a
    trailing comma in an enum { ... } list.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Sep 5, 2006
    #5
  6. Old Wolf said:

    > Robert Gamble wrote:
    >> wrote:
    >> > char *testCharArray[] = {
    >> > "one",
    >> > "two",
    >> > "three",
    >> > "four",
    >> > "five",
    >> > "six",
    >> > NULL,
    >> > };

    >>
    >> > and why should there be a comma after NULL ?

    >>
    >> Trailing commas at the end of an initializer list are a feature
    >> intended to aid automatic code generators, they are not required.

    >
    > To be clearer, this trailing comma means the program is
    > not a well-formed C90 program -- although it is a very common
    > compiler extension to allow it.


    Not so. The grammar (taken from 3.5.7) is:

    initializer:
    assignment-expression
    { initializer-list }
    { initializer-list , }

    initializer-list:
    initializer
    initializer-list , initializer

    from which it is clear that a trailing comma is perfectly legal in C90.
    Indeed, the Standard gives (at least) a couple of examples of this usage:

    float y[4][3] = {
    { 1, 3, 5 },
    { 2, 4, 6 },
    { 3, 5, 7 },
    };

    and

    short q[4][3][2] = {
    {
    { 1 },
    },
    {
    { 2, 3 },
    },
    {
    { 4, 5 },
    { 6 },
    }
    };

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Sep 5, 2006
    #6
  7. Old Wolf wrote:
    > Robert Gamble wrote:
    > > wrote:
    > > > char *testCharArray[] = {
    > > > "one",
    > > > "two",
    > > > "three",
    > > > "four",
    > > > "five",
    > > > "six",
    > > > NULL,
    > > > };

    > >
    > > > and why should there be a comma after NULL ?

    > >
    > > Trailing commas at the end of an initializer list are a feature
    > > intended to aid automatic code generators, they are not required.

    >
    > To be clearer, this trailing comma means the program is
    > not a well-formed C90 program -- although it is a very common
    > compiler extension to allow it.


    This has been allowed since K&R 1.

    Robert Gamble.
     
    Robert Gamble, Sep 5, 2006
    #7
  8. Richard Bos Guest

    CBFalconer <> wrote:

    > > wrote:
    > >
    > >> char *testCharArray[] = {
    > >> "one",
    > >> "two",
    > >> "three",
    > >> "four",
    > >> "five",
    > >> "six",
    > >> NULL,
    > >> };


    > IMO a better way to write that declaration (and be compatible with
    > all C versions) is:
    >
    > char *testCharArray[] = { "one"
    > , "two"
    > , "three"
    > , "four"
    > , "five"
    > , "six"
    > , NULL
    > };


    It works (although it is no more compatible with C89 than the first
    one), but my goodness it's ugly.

    Richard
     
    Richard Bos, Sep 5, 2006
    #8
  9. Ben Pfaff Guest

    (Richard Bos) writes:

    > CBFalconer <> wrote:
    >
    >> > wrote:
    >> >
    >> >> char *testCharArray[] = {
    >> >> "one",
    >> >> "two",
    >> >> "three",
    >> >> "four",
    >> >> "five",
    >> >> "six",
    >> >> NULL,
    >> >> };

    >
    >> IMO a better way to write that declaration (and be compatible with
    >> all C versions) is:
    >>
    >> char *testCharArray[] = { "one"
    >> , "two"
    >> , "three"
    >> , "four"
    >> , "five"
    >> , "six"
    >> , NULL
    >> };

    >
    > It works (although it is no more compatible with C89 than the first
    > one), but my goodness it's ugly.


    Both forms are compatible with C89, and C99 also for that matter.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Sep 5, 2006
    #9
  10. CBFalconer Guest

    Ben Pfaff wrote:
    > (Richard Bos) writes:
    >> CBFalconer <> wrote:
    >>>> wrote:
    >>>>
    >>>>> char *testCharArray[] = {
    >>>>> "one",
    >>>>> "two",

    ....
    >>>>> "six",
    >>>>> NULL,
    >>>>> };

    >>
    >>> IMO a better way to write that declaration (and be compatible
    >>> with all C versions) is:
    >>>
    >>> char *testCharArray[] = { "one"
    >>> , "two"

    ....
    >>> , "six"
    >>> , NULL
    >>> };

    >>
    >> It works (although it is no more compatible with C89 than the
    >> first one), but my goodness it's ugly.

    >
    > Both forms are compatible with C89, and C99 also for that matter.


    Oh? I was under the impression that the final comma in the first
    version was an error under C89. The objective is to have an easily
    modifiable initialization.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Sep 5, 2006
    #10
  11. Ben Pfaff Guest

    CBFalconer <> writes:

    > Ben Pfaff wrote:
    >> (Richard Bos) writes:
    >>> CBFalconer <> wrote:
    >>>>> wrote:
    >>>>>
    >>>>>> char *testCharArray[] = {
    >>>>>> "one",
    >>>>>> "two",

    > ...
    >>>>>> "six",
    >>>>>> NULL,
    >>>>>> };
    >>>
    >>>> IMO a better way to write that declaration (and be compatible
    >>>> with all C versions) is:
    >>>>
    >>>> char *testCharArray[] = { "one"
    >>>> , "two"

    > ...
    >>>> , "six"
    >>>> , NULL
    >>>> };
    >>>
    >>> It works (although it is no more compatible with C89 than the
    >>> first one), but my goodness it's ugly.

    >>
    >> Both forms are compatible with C89, and C99 also for that matter.

    >
    > Oh? I was under the impression that the final comma in the first
    > version was an error under C89.


    No. You are confusing initializers with enum { ... } lists,
    which C99 did change to allow a trailing comma.

    > The objective is to have an easily modifiable initialization.


    Yes.
    --
    Here's a tip: null pointers don't have to be *dull* pointers!
     
    Ben Pfaff, Sep 6, 2006
    #11
  12. Richard Bos Guest

    CBFalconer <> wrote:

    > Ben Pfaff wrote:
    > > (Richard Bos) writes:
    > >> CBFalconer <> wrote:
    > >>>> wrote:
    > >>>>
    > >>>>> char *testCharArray[] = {
    > >>>>> "one",
    > >>>>> "two",

    > ...
    > >>>>> "six",
    > >>>>> NULL,
    > >>>>> };
    > >>
    > >>> IMO a better way to write that declaration (and be compatible
    > >>> with all C versions) is:
    > >>>
    > >>> char *testCharArray[] = { "one"
    > >>> , "two"

    > ...
    > >>> , "six"
    > >>> , NULL
    > >>> };
    > >>
    > >> It works (although it is no more compatible with C89 than the
    > >> first one), but my goodness it's ugly.

    > >
    > > Both forms are compatible with C89, and C99 also for that matter.

    >
    > Oh? I was under the impression that the final comma in the first
    > version was an error under C89.


    No. Your form is neither more nor less compatible with C99 than
    's.

    > The objective is to have an easily modifiable initialization.


    That's one reason why the trailing comma was already allowed in C89. I
    suspect the illegality of trailing commas in enum declarations was a
    simple oversight.

    Richard
     
    Richard Bos, Sep 6, 2006
    #12
  13. pete Guest

    Robert Gamble wrote:
    >
    > wrote:
    > > char *testCharArray[] = {
    > > "one",
    > > "two",
    > > "three",
    > > "four",
    > > "five",
    > > "six",
    > > NULL,
    > > };
    > >
    > > The above code initializes a NULL terminated char* array.
    > > The question
    > > is why is a semi-colon needed after the bracket (ie. };)

    >
    > Because declarations in C end with a semicolon,


    All declarations in C end with a semicolon,
    except function definitions.

    > the closing bracket signifies
    > the end of the initializer list,
    > not the end of the declaration.


    --
    pete
     
    pete, Sep 8, 2006
    #13
    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. Roy Smith
    Replies:
    2
    Views:
    1,910
    Peter Otten
    Mar 6, 2004
  2. Anders Christensen

    char array not terminated with \0

    Anders Christensen, Jan 16, 2005, in forum: C Programming
    Replies:
    17
    Views:
    612
    Anders Christensen
    Jan 25, 2005
  3. Replies:
    14
    Views:
    505
  4. ssylee
    Replies:
    4
    Views:
    505
    CBFalconer
    Aug 12, 2008
  5. Replies:
    8
    Views:
    578
    Barry Schwarz
    Aug 31, 2012
Loading...

Share This Page