struct question: NULL terminated char* array

C

chy1013m1

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
 
R

Robert Gamble

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
 
C

CBFalconer

Robert said:
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:
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
 
O

Old Wolf

Robert said:
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.
 
B

Ben Pfaff

Old Wolf said:
Robert said:
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.
 
R

Richard Heathfield

Old Wolf said:
Robert said:
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 },
}
};
 
R

Robert Gamble

Old said:
Robert said:
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.
 
R

Richard Bos

CBFalconer said:
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
 
B

Ben Pfaff

CBFalconer said:
(e-mail address removed) 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.
 
C

CBFalconer

Ben said:
CBFalconer said:
(e-mail address removed) 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.
 
B

Ben Pfaff

CBFalconer said:
Ben said:
(e-mail address removed) 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.
 
R

Richard Bos

CBFalconer said:
Ben said:
(e-mail address removed) 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
(e-mail address removed)'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
 
P

pete

Robert said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top