允许初始化程序结束时使用逗号? [英] comma at end of initializer allowed?

查看:84
本文介绍了允许初始化程序结束时使用逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否允许以下​​代码?我在初始化器中的

最后一个函数之后讨论逗号。


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

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


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


struct Funcs funcs =

{

f,

g,

};


我问,因为它使生成代码变得更容易。

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.

推荐答案

On星期二,2004年4月20日09:10:49 +0200,Serve Laurijssen

< cs@nospam.comp.com>写道:
On Tue, 20 Apr 2004 09:10:49 +0200, "Serve Laurijssen"
<cs@nospam.comp.com> wrote:
是否允许以下​​代码?我在初始化器中的最后一个函数之后讨论逗号。

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

struct Funcs {void(* f [])(void);结构功能funcs =
{
f,
g,
};

我问,因为它使生成代码要容易得多。
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.




我相信它是(而且IIRC的理由正是因为这个原因)。但是,避免单个if语句真的是一件大事吗,

?:或返回buf + x?


我写了很多代码生成以逗号分隔的列表(以及

,\ n分隔行和FF分隔的页面),这些列表设法用几个字节的简单代码来处理问题。

-

Sev



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


Severian< se ****** @ chlamydia-is-not -a-flower.com>写道:

:2004年4月20日星期二09:10:49 +0200,Serve Laurijssen

:< cs@nospam.comp.com>写道:

:>是否允许以下​​代码?我正在谈论

:>初始化程序中的最后一个函数后的逗号。

:>

:> void f( void){puts(" f");}

:> void g(void){puts(" g");}

:>

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

:>

:> struct Funcs funcs =

:> {

:> ; f,

:> g,

:>};

:>

:> ;我问,因为它使生成代码变得更容易。

:>


另外,你可以在最后放置一个虚拟元素或一个空指针,

例如


struct Funcs funcs =

{

f,

g,

NULL

}
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
}


2004年4月20日星期二11:25:30 GMT,Ike Naar< no **** @ nospam.invalid>

写道:
On Tue, 20 Apr 2004 11:25:30 GMT, Ike Naar <no****@nospam.invalid>
wrote:
Severian< se ****** @ chlamydia-is- not-a-flower.com>写道:
:2004年4月20日星期二09:10:49 +0200,Serve Laurijssen
:< cs@nospam.comp.com>写道:
:>是否允许以下​​代码?我在初始化器中的
:>最后一个函数之后讨论逗号。
:>
:> void f(void){puts(" f");}}
:> void g(void){puts(" g");}
:>
:> struct Funcs {void(* f [])(void); };
:>
:> struct Funcs funcs =


昨晚我没注意到,但这显然需要


struct Funcs funcs [] =

:> {
:> f,
:> g,
:>};
:>
:>我问,因为它使生成代码变得更加容易。
:>


<我相信IKE偶然在这里剪了一些东西。>

另外,你可以在末尾放一个虚拟元素或空指针,
例如

struct Funcs funcs =
{
f,
g,
NULL
}
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
}



是的,如果处理

代码以这种方式编写,并且浪费的存储空间微不足道,这有时是适当的做法。


有时候你无法控制它;我的很多代码生成

列表是用于其他方式(CSV文件,SQL查询等),其中

不允许多余的逗号或' 'NULL。''最好学习好方法

生成这样的列表,而不依赖于历史C编译器

津贴。开销很小,几乎总是被忽略。

-

Sev



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


这篇关于允许初始化程序结束时使用逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆