char * testCharArray [] = { " one", " two", " three", 四, 五, 六, NULL, }; 上面的代码初始化一个NULL终止的char *数组。问题 是为什么括号后需要一个分号(即。};),以及为什么 应该在NULL之后有逗号? (我在书中看到开始linux 编程第3版。在auther之后附加一个逗号,删除 逗号对代码没有任何作用) $我使用的b $ b编译器是gcc 3.4.4
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
推荐答案chy101 ... @ gmail写道: chy101...@gmail wrote: char * testCharArray [] = { " one", " two", 三, 四, 五, 六, NULL, }; 上面的代码初始化一个NULL终止的char *数组。问题 是为什么在括号后需要一个分号(即。};) 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. };)
因为C中的声明以a结尾分号,结束括号 表示初始化列表的结尾,而不是 声明的结尾。
Because declarations in C end with a semicolon, the closing bracket signifies the end of the initializer list, not the end of the declaration.
和为什么在NULL之后会有逗号? (我在书中看到开始 linux编程第3版。在auther之后附加一个逗号, 删除逗号对代码没有任何作用) 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)初始化列表末尾的尾随逗号是一个功能 旨在帮助自动代码生成器,它们不是必需的。 Robert Gamble
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写道: Robert Gamble wrote: chy101 ... @ gmail写道: chy101...@gmail wrote: > char * testCharArray [] = {" one"," two",three,four,five,six, NULL,}; >char *testCharArray[] = { "one", "two", "three", "four", "five", "six", NULL,};
.... snip ...
.... snip ...
> > >为什么在NULL之后会有逗号? (我在书中看到开始linux编程第3版。在auther之后附加一个逗号,删除逗号后对代码不做任何操作) >and why should there be a comma after NULL ? (I saw in the bookbeginning linux programming 3rd ed. the auther appended a commaafter 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更好的方式来编写该声明(并且与所有C版本的b / b兼容)是: char * testCharArray [] = {" one" ," two" ,"" ,四 ,五 ,六 ,NULL } ; - 一些信息链接: news:news.announce.newusers www.geocities/nnqweb/ www.catb/~esr/faqs/smart-questions .html www.caliburn.nl/ topposting.html http://wwwmeister .ORG /新闻/ learn2quote .html
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 www.geocities/nnqweb/ www.catb/~esr/faqs/smart-questions.html www.caliburn.nl/topposting.html wwwmeister/news/learn2quote.html
Robert Gamble写道: Robert Gamble wrote: chy101 ... @ gmail写道: chy101...@gmail wrote: char * testCharArray [] = { " one", " two", 三, 四, " five" " six" ;, NULL, }; char *testCharArray[] = { "one", "two", "three", "four", "five", "six", NULL, };
以及为什么在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.
为了更清楚,这个尾随的逗号意味着该程序是 而不是一个结构良好的C90程序 - 虽然这是一个非常普遍的 编译器扩展允许它。
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.
更多推荐
struct question:NULL终止char *数组
发布评论