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 12 2583
chy101...@gmail.com 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 wrote:
chy101...@gmail.com 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
Robert Gamble wrote:
chy101...@gmail.com 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 said:
Robert Gamble wrote:
>chy101...@gmail.com 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)
"Old Wolf" <ol*****@inspire.net.nzwrites:
Robert Gamble wrote:
>chy101...@gmail.com 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Old Wolf wrote:
Robert Gamble wrote:
chy101...@gmail.com 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.
CBFalconer <cb********@yahoo.comwrote:
chy101...@gmail.com 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 rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
CBFalconer <cb********@yahoo.comwrote:
chy101...@gmail.com 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Ben Pfaff wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>CBFalconer <cb********@yahoo.comwrote:
>>>chy101...@gmail.com 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 <cb********@yahoo.comwrites:
Ben Pfaff wrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>>CBFalconer <cb********@yahoo.comwrote: chy101...@gmail.com 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!
CBFalconer <cb********@yahoo.comwrote:
Ben Pfaff wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
CBFalconer <cb********@yahoo.comwrote: chy101...@gmail.com 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 ch*******@gmail.com'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
Robert Gamble wrote:
>
chy101...@gmail.com 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Robert Sturzenegger |
last post by:
We had a strange problem in a large project with the use of a string class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see
below)....
|
by: Alan |
last post by:
hi all,
I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a constant
value, say a
below is my code but it fails
what is the correct...
|
by: ehabaziz2001 |
last post by:
Hi,
Till now I do not understand how the null character automatically added
to the end of the string and it is not an element of the string array .
All books said the null character (\0) added...
|
by: Alfonso Morra |
last post by:
Hi,
I am at the end of my tether now - after spending several days trying to
figure how to do this. I have finally written a simple "proof of
concept" program to test serializing a structure...
|
by: Tiger |
last post by:
I try to write a struct into a brut file but I can't write all var in
this struct
when I try to add user I have that :
> testmachine:/usr/share/my_passwd# ./my_passwd -a users.db
> Ajout d'une...
|
by: mast2as |
last post by:
Hi everyone,
I am trying to implement some specs which specify that an array of
parameter is passed to a function as a pointer to an array terminated
by a NULL chatacter. That seemed fairly easy...
|
by: Antti Karanta |
last post by:
Hi!
Is it possible to inline initialize a struct whose one member is a string
array of arbitrary length (terminated w/ a NULL ptr)? What I mean is
something like this:
typedef struct {...
|
by: mdh |
last post by:
I am still having a problem understanding K&RII on p 112. I have
looked at the FAQs --which I am sure answer it in a way that I have
missed, so here goes.
A 2-dim array, (per K&R) is really a...
|
by: Mike Margerum |
last post by:
I am sending a C struct from a C app via sockets to a C# server. I
occasionally get an error when reading a C char array using the
BinaryReader Class. the error is:
"The output char buffer is...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |