user-0 提问: 结构体初始化问题

回复:
user: user-1
content: 引用 12 楼 zhao4zhong1 的回复:[quote=引用 10 楼 sulu031351 的回复:] [quote=引用 8 楼 zhao4zhong1 的回复:] stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f},               {1,3f}, };                    {1,4f},                       };   stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f}                {1,3f}, };                    {1,4f}                       };  这个是想说追加变量时的操作？[/quote] 当分多行写时，每行后面都有逗号，格式整齐划一，方便添加新项。 当不分多行写时，最后一项后面没有逗号更自然。  所以C语法规定最后一项后面的逗号可有可无。[/quote] 正解！

user: user-2
content: 15楼又把这个帖子翻出来了，楼主应该是csdn一账号一贴游，  在末尾加逗号好像是七八十年代的一种编程习惯

user: user-3
content: 引用 楼主 sulu031351 的回复:struct student {  int i1;  float f1; }stu; 以下两种初始化区别在于最后一项加不加逗号，编译不出问题，请问有什么区别 stu[ ] = { {1,1},{1,2},{1,3},};  stu[ ] = { {1,1},{1,2},{1,3}};  这种问题关键就是看下结构数组的大小  sizeof(stu) 看下大小，如果大小一样的话，那没啥区别

user: user-4
content: 真的是一个可有可无的东西。

user: user-5
content: 可有可无，当初也纠结过

user: user-6
content: 我看到的一个说法是：当时考虑到自动生成代码的问题，（自动生成代码容易）所以编译器就接受了这2种方案。

user: user-7
content: 引用 12 楼 zhao4zhong1 的回复:[quote=引用 10 楼 sulu031351 的回复:] [quote=引用 8 楼 zhao4zhong1 的回复:] stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f},               {1,3f}, };                    {1,4f},                       };   stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f}                {1,3f}, };                    {1,4f}                       };  这个是想说追加变量时的操作？[/quote] 当分多行写时，每行后面都有逗号，格式整齐划一，方便添加新项。 当不分多行写时，最后一项后面没有逗号更自然。  所以C语法规定最后一项后面的逗号可有可无。[/quote]+++++++++++

user: user-1
content: Initializers Declarators can specify the initial value for objects. The only way to specify a value for objects of const type is in the declarator. The part of the declarator that specifies this initial value is called the “initializer.”  Syntax  initializer :  = assignment-expression = { initializer-list ,opt } ( expression-list )  initializer-list :  expression initializer-list , expression { initializer-list ,opt }  There are two fundamental types of initializers:   The initializer invoked using the equal-sign syntax   The initializer invoked using function-style syntax  Only objects of classes with constructors can be initialized with the function-style syntax. The two syntax forms also differ in access control and in the potential use of temporary objects. Consider the following code, which illustrates some declarators with initializers:  int      i = 7;                  // Uses equal-sign syntax. Customer Cust( "Taxpayer, Joe",  // Uses function-style                "14 Cherry Lane", //  syntax. Requires presence                "Manteca",        //  of a constructor.                "CA" );  Declarations of automatic, register, static, and external variables can contain initializers. However, declarations of external variables can contain initializers only if the variables are not declared as extern.  These initializers can contain expressions involving constants and variables in the current scope. The initializer expression is evaluated at the point the declaration is encountered in program flow, or, for global static objects and variables, at program startup. (For more information about initialization of global static objects, see Additional Startup Considerations in Chapter 2.)

user: user-1
content: 引用 10 楼 sulu031351 的回复:[quote=引用 8 楼 zhao4zhong1 的回复:] stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f},               {1,3f}, };                    {1,4f},                       };   stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f}                {1,3f}, };                    {1,4f}                       };  这个是想说追加变量时的操作？[/quote] 当分多行写时，每行后面都有逗号，格式整齐划一，方便添加新项。 当不分多行写时，最后一项后面没有逗号更自然。  所以C语法规定最后一项后面的逗号可有可无。

user: user-0
content: 引用 7 楼 Intel0011 的回复:这两种情况都符合标准 区别在于 stu[ ] = { {1,1},{1,2},{1,3},}; 和 stu[ ] = { {1,1},{1,2},{1,3}}; 前者添加新成员时不必写逗号，不易出错 后者添加新成员时容易忘记写逗号，编译出错 标准上什么地方提到stu[ ] = { {1,1},{1,2},{1,3},};这个也符合？

user: user-0
content: 引用 8 楼 zhao4zhong1 的回复:stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f},               {1,3f}, };                    {1,4f},                       };   stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f}                {1,3f}, };                    {1,4f}                       };  这个是想说追加变量时的操作？

user: user-0
content: 引用 5 楼 zxh707wk 的回复:自己编译一下不就知道了吗 能自己动手实践的就不要问别人，i因为可能会有错误回答 说了编译不出问题的，所以想问下对编译原来理解的人，看下是什么原因

user: user-1
content: stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f},               {1,3f}, };                    {1,4f},                       };   stu[ ] = {            stu[ ] = { {1,1f},               {1,1f}, {1,2f},      ==>      {1,2f}, {1,3f}                {1,3f}, };                    {1,4f}                       };

user: user-8
content: 这两种情况都符合标准 区别在于 stu[ ] = { {1,1},{1,2},{1,3},}; 和 stu[ ] = { {1,1},{1,2},{1,3}}; 前者添加新成员时不必写逗号，不易出错 后者添加新成员时容易忘记写逗号，编译出错

user: user-9
content: 没区别，初始化列表最终可以多个逗号，方便自动代码生成。

user: user-10
content: 自己编译一下不就知道了吗 能自己动手实践的就不要问别人，i因为可能会有错误回答

user: user-11
content: 没区别。C、C++为了方便，允许最后多余一个，

user: user-12
content: 应该有差别的 你可以看下内存大小 有逗号的应该会多分配一个结构体的大小。

user: user-13
content: 对编译器来说没有区别。

user: user-14
content: 没啥区别