473,396 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

comma at end of initializer allowed?

Is code like the following allowed? I am talking about the comma after the
last function in the initializer.

void f(void) {puts("f");}
void g(void) {puts("g");}

struct Funcs { void (*f[])(void); };

struct Funcs funcs =
{
f,
g,
};

I ask because it makes generating code a lot easier.
Nov 14 '05 #1
12 5099
On Tue, 20 Apr 2004 09:10:49 +0200, "Serve Laurijssen"
<cs@nospam.comp.com> wrote:
Is code like the following allowed? I am talking about the comma after the
last function in the initializer.

void f(void) {puts("f");}
void g(void) {puts("g");}

struct Funcs { void (*f[])(void); };

struct Funcs funcs =
{
f,
g,
};

I ask because it makes generating code a lot easier.


I believe it is (and IIRC the rationale was for precisely this
reason). But is it really a big deal to avoid a single if statement,
?: or return buf+x?

I've written lots of code that generates comma-separated lists (and
,\n separated lines, and FF separated pages) that managed to handle
the issue with a few bytes of simple code.
--
Sev
Nov 14 '05 #2
Severian <se******@chlamydia-is-not-a-flower.com> wrote:
: On Tue, 20 Apr 2004 09:10:49 +0200, "Serve Laurijssen"
: <cs@nospam.comp.com> wrote:
:>Is code like the following allowed? I am talking about the comma after the
:>last function in the initializer.
:>
:>void f(void) {puts("f");}
:>void g(void) {puts("g");}
:>
:>struct Funcs { void (*f[])(void); };
:>
:>struct Funcs funcs =
:>{
:>f,
:>g,
:>};
:>
:>I ask because it makes generating code a lot easier.
:>

Alternatively, you can put a dummy element or a null pointer at the end,
e.g.

struct Funcs funcs =
{
f,
g,
NULL
}
Nov 14 '05 #3
On Tue, 20 Apr 2004 11:25:30 GMT, Ike Naar <no****@nospam.invalid>
wrote:
Severian <se******@chlamydia-is-not-a-flower.com> wrote:
: On Tue, 20 Apr 2004 09:10:49 +0200, "Serve Laurijssen"
: <cs@nospam.comp.com> wrote:
:>Is code like the following allowed? I am talking about the comma after the
:>last function in the initializer.
:>
:>void f(void) {puts("f");}
:>void g(void) {puts("g");}
:>
:>struct Funcs { void (*f[])(void); };
:>
:>struct Funcs funcs =
I didn't notice late last night, but this obviously needs to be

struct Funcs funcs[] =
:>{
:>f,
:>g,
:>};
:>
:>I ask because it makes generating code a lot easier.
:>
<I believe IKE snipped something here by accident.>
Alternatively, you can put a dummy element or a null pointer at the end,
e.g.

struct Funcs funcs =
{
f,
g,
NULL
}


Yes, this is sometimes the appropriate thing to do, if the processing
code is written that way, and if the wasted storage is insignificant.

Sometimes you don't have control over it; a lot of my code to generate
lists is for use in other ways (CSV files, SQL queries, etc.), which
do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances. The overhead is so minimal that it can almost always be
ignored.
--
Sev
Nov 14 '05 #4
In <sr********************************@4ax.com> Severian <se******@chlamydia-is-not-a-flower.com> writes:
do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances.


It's not historical, it's a C89 invention.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
On 20 Apr 2004 12:49:50 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <sr********************************@4ax.com> Severian <se******@chlamydia-is-not-a-flower.com> writes:
do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances.


It's not historical, it's a C89 invention.


I may very well be mistaken [1989 was a long time ago on my brief
timeline (b. 1961), I've written millions of lines of code, and I'm
unlikely to spend any time researching the issue], but I remember that
this became part of the standard because many extant C compilers
already accepted the syntax.

I remember it as more a concession to YACC (or some other code
generator) output than anything else.

I know that VAX C (not a great example of a C compiler, but DEC was a
powerful member of the committee) and several PC C compilers handled
it as it was standardized.

(I will have no problem saying "Oops, my bad" if necessary, because
this is totally from memory and I have no emotional attachment to it.)

--
Sev
Nov 14 '05 #6
>On 20 Apr 2004 12:49:50 GMT, Da*****@cern.ch (Dan Pop) wrote:
It's not historical, it's a C89 invention.

(Dan is rarely wrong, but I have some vague memory of Dennis'
compilers accepting trailing commas in initializers.)

In article <news:3m********************************@4ax.com >
Severian <se******@chlamydia-is-not-a-flower.com> writes (in part):I remember it as more a concession to YACC (or some other code
generator) output than anything else.


This seems unlikely, because PCC (the first YACC-based C compiler)
accepted trailing commas in initializers but not in "enum"s. It
is easy enough to write one's grammar either way.

We might even note that trailing commas were still not valid in
"enum"s in C89 (they finally are, in C99).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #7

On Tue, 20 Apr 2004, Severian wrote:

Ike Naar <no****@nospam.invalid> wrote:
Severian <se******@chlamydia-is-not-a-flower.com> wrote:
: "Serve Laurijssen" <cs@nospam.comp.com> wrote:
:> Is code like the following allowed? I am talking about the comma
:> after the last function in the initializer.
:>
:> void f(void) {puts("f");}
:> void g(void) {puts("g");}
:>
:> struct Funcs { void (*f[])(void); };
:>
:> struct Funcs funcs =
I didn't notice late last night, but this obviously needs to be

struct Funcs funcs[] =


No, it doesn't. Look closely at the typedef for 'Funcs'. :)
I don't follow C99 weird-array syntax closely enough to tell if
that typedef is legal, but GNU C accepts it with a warning. (This
means it's probably not standard C. Add a '2' inside the '[]' in
the typedef if you want to compile it.)
:>{
:>f,
:>g,
:>};
*This*, however, definitely needs to be

{ {f,g,} };

(two pairs of braces). One pair for the struct and an inner pair for
the initializer for the array member.
:>
:>I ask because it makes generating code a lot easier. Alternatively, you can put a dummy element or a null pointer at the end,
e.g.

struct Funcs funcs =
{
f,
g,
NULL
}


Yes, this is sometimes the appropriate thing to do, if the processing
code is written that way, and if the wasted storage is insignificant.


Just in case you didn't see it (I can't tell from your response),
I think Ike's point was that if your code-generator dumps a bunch
of

xxx,

followed by the string

NULL
}

then you don't have to worry about the extra trailing comma. The
NULL itself can be ignored --- /if the processing code is written
that way/, of course. ;-)
Sometimes you don't have control over it; a lot of my code to generate
lists is for use in other ways (CSV files, SQL queries, etc.), which
do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances. The overhead is so minimal that it can almost always be
ignored.


Modulo Dan's comments about "historical" (hey, 1989 sounds pretty
historical to *me*! ;) , you're right.

-Arthur
Nov 14 '05 #8
In <3m********************************@4ax.com> Severian <se******@chlamydia-is-not-a-flower.com> writes:
On 20 Apr 2004 12:49:50 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <sr********************************@4ax.com> Severian <se******@chlamydia-is-not-a-flower.com> writes:
do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances.


It's not historical, it's a C89 invention.


I may very well be mistaken [1989 was a long time ago on my brief
timeline (b. 1961), I've written millions of lines of code, and I'm
unlikely to spend any time researching the issue], but I remember that
this became part of the standard because many extant C compilers
already accepted the syntax.


It turns out I was wrong: it was a standard C feature since "day one",
i.e. since K&R1 was printed. Retained by the standardisation committee
because "it provides flexibility in adding or deleting members from
an initializer list, and simplifies machine generation of such lists".

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
Dan Pop a écrit :
In <sr********************************@4ax.com> Severian <se******@chlamydia-is-not-a-flower.com> writes:

do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances.

It's not historical, it's a C89 invention.


C89 belongs to C history.

--
Richard
Nov 14 '05 #10
In <40**********************@news.club-internet.fr> Richard Delorme <ab****@nospam.fr> writes:
Dan Pop a écrit :
In <sr********************************@4ax.com> Severian <se******@chlamydia-is-not-a-flower.com> writes:

do not allow for excess commas or 'NULL.' It's best to learn good ways
to generate such lists without depending on historical C compiler
allowances.

It's not historical, it's a C89 invention.


C89 belongs to C history.


To the same extent that C99 does.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
Serve Laurijssen wrote:
Is code like the following allowed? I am talking about the comma after the
last function in the initializer.

void f(void) {puts("f");}
void g(void) {puts("g");}

struct Funcs { void (*f[])(void); };

struct Funcs funcs =
{
f,
g,
};

I ask because it makes generating code a lot easier.


Hardly. *Slightly* easier, I'd buy. My standard idiom for this thinks
of the separator as being *before* each element, not *after*, with
a special case for the first element, and looks like

separator = nothing or a space or other I'm-not-here
for each item x do
output the separator
output x
separator = the non-trivial separator
endfor

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #12
Chris Dollin wrote:
Serve Laurijssen wrote:
Is code like the following allowed? I am talking about the comma after the
last function in the initializer.
(snip)
struct Funcs funcs =
{
f,
g,
}; I ask because it makes generating code a lot easier.

Hardly. *Slightly* easier, I'd buy.


However slight, it is legal in C and Java.

If is helpful if using #ifdef within the initializer,
when you don't know which items will be used.

Also, for initializers generated by a program, though in
that case it isn't so hard to fix up the last item.

-- glen

Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
2
by: Pmb | last post by:
I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to...
6
by: Clint Olsen | last post by:
What about the following is not computable? It seems that the size of foo is easily computable: typedef struct { char *name; char *data; } Foo; int main(void) {
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
2
by: cody | last post by:
class Test { IList list = new ArrayList(); MyCollection list2 = new MyCollection (list); } Leads to this error. I know I could initialize them in the ctor but I'm asking myself where this...
1
by: mr.gsingh | last post by:
My program looks something like this: int x = 23; if ( (func(x), func1(x), func2(x)) <= (foo(x), foo1(x), foo2(x))) { std::cout << "............"; } else { std::cout<< "..............";
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
22
by: aarklon | last post by:
Hi all, why does C language permits an extra comma in initializer list ex:- int days = { 31,28.31,30,31,30, 31,31,30,31,30,31, } i have heard it is for the purpose of automatic code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.