user-0 提问: 结构体赋值

回复:
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-0
content: 引用 8楼paschen 的回复:引用 5 楼 loveqise的回复:[quote=引用 1楼赵4老师 的回复:]编译器不支持。这个是C99吗？ VS不支持C99标准  像C99中的VLA也是不支持的[/quote]我用三楼方法，｛｝里面为空报错，里面有参数才行，这是为什么

user: user-0
content: 引用 3楼_zSir 的回复:如果是要初始化第二个元素，可以这样写：  #include <stdio.h> #include <string.h>  typedef struct lens {     float foclen;     float fstop;     char brand[30]; }LENS;  int main(void) {     LENS str1[10] = {         {},         {},         {500, 2, "Remarkatar"}     };      int i;     for(i = 0; i < 10; i++) {         printf("[%d] %.2f, %.2f, %s\n",                 i,                 str1[i].foclen,                 str1[i].fstop,                 str1[i].brand);     }      return 0; }  测试结果：  [0] 0.00, 0.00,  [1] 0.00, 0.00,  [2] 500.00, 2.00, Remarkatar [3] 0.00, 0.00,  [4] 0.00, 0.00,  [5] 0.00, 0.00,  [6] 0.00, 0.00,  [7] 0.00, 0.00,  [8] 0.00, 0.00,  [9] 0.00, 0.00, 我用vs，｛｝里面为空编译出错，里面

user: user-2
content: 引用 5 楼 loveqise的回复:引用 1楼赵4老师 的回复:编译器不支持。这个是C99吗？ VS不支持C99标准  像C99中的VLA也是不支持的

user: user-0
content: 引用 4楼AlbertS 的回复:没有这种写法，要是想初始化第3个元素，后面可以不写，前两个必须补全，参考一下3楼哦应该是C99吧

user: user-0
content: 引用 2楼小灸舞 的回复:没有这种用法应该是C99吧

user: user-0
content: 引用 1楼赵4老师 的回复:编译器不支持。这个是C99吗？

user: user-3
content: 没有这种写法，要是想初始化第3个元素，后面可以不写，前两个必须补全，参考一下3楼哦

user: user-4
content: 如果是要初始化第二个元素，可以这样写：  #include <stdio.h> #include <string.h>  typedef struct lens {     float foclen;     float fstop;     char brand[30]; }LENS;  int main(void) {     LENS str1[10] = {         {},         {},         {500, 2, "Remarkatar"}     };      int i;     for(i = 0; i < 10; i++) {         printf("[%d] %.2f, %.2f, %s\n",                 i,                 str1[i].foclen,                 str1[i].fstop,                 str1[i].brand);     }      return 0; }  测试结果：  [0] 0.00, 0.00,  [1] 0.00, 0.00,  [2] 500.00, 2.00, Remarkatar [3] 0.00, 0.00,  [4] 0.00, 0.00,  [5] 0.00, 0.00,  [6] 0.00, 0.00,  [7] 0.00, 0.00,  [8] 0.00, 0.00,  [9] 0.00, 0.00,

user: user-5
content: 没有这种用法

user: user-1
content: 编译器不支持。