user-0 提问: c语言结构体初始化错误

回复:
user: user-1
content: C编译器，结构体初始化赋值才能定义赋值 需要如下这样： struct timeval time_start = time_end = time1 = time2 = time3 = time4 = time5 = {0，0};

user: user-2
content: 好像结构体不能这样直接赋值，看看大神怎么解释。。。混分

user: user-3
content: Summary of Statements statement :  labeled-statement compound-statement expression-statement selection-statement iteration-statement jump-statement try-except-statement   /* Microsoft Specific */ try-finally-statement   /* Microsoft Specific */  jump-statement :  goto identifier ; continue; break; return expression opt ;  compound-statement :  { declaration-list opt statement-list opt }  declaration-list :  declaration declaration-list declaration  statement-list :  statement statement-list statement  expression-statement :  expression opt ;  iteration-statement :  while ( expression ) statement do statement while ( expression ); for ( expression opt ; expression opt ; expression opt ) statement  selection-statement :  if ( expression ) statement if ( expression ) statement else statement switch ( expression ) statement  labeled-statement :  identifier : statement case constant-expression : statement default : statement  try-except-statement :   /* Microsoft Specific */  __try compound-statement __except ( expression ) compound-statement  try-finally-statement :   /* Microsoft Specific */  __try compound-statement __finally compound-statement

user: user-3
content: Summary of Declarations declaration :  declaration-specifiers attribute-seq opt init-declarator-list opt ;                         /* attribute-seq is Microsoft Specific */  declaration-specifiers :  storage-class-specifier declaration-specifiers opt type-specifier declaration-specifiers opt type-qualifier declaration-specifiers opt  attribute-seq :            /* attribute-seq is Microsoft Specific */  attribute attribute-seq opt  attribute : one of      /* Microsoft Specific */  __asm __fastcall  __based __inline  __cdecl __stdcall    init-declarator-list :  init-declarator init-declarator-list , init-declarator  init-declarator :  declarator declarator = initializer /* For scalar initialization */  storage-class-specifier :  auto register static extern typedef __declspec ( extended-decl-modifier-seq ) /* Microsoft Specific */  type-specifier :  void char short int __int8       /* Microsoft Specific */ __int16      /* Microsoft Specific */ __int32      /* Microsoft Specific */ __int64      /* Microsoft Specific */ long float double signed unsigned struct-or-union-specifier enum-specifier typedef-name  type-qualifier :  const volatile  declarator :  pointer opt direct-declarator  direct-declarator :  identifier ( declarator ) direct-declarator [ constant-expression opt] direct-declarator ( parameter-type-list )   /* New-style declarator */ direct-declarator ( identifier-list opt )      /* Obsolete-style declarator */  pointer :  * type-qualifier-list opt * type-qualifier-list opt pointer  parameter-type-list :                           /* The parameter list */  parameter-list parameter-list , ...  parameter-list :  parameter-declaration parameter-list , parameter-declaration  type-qualifier-list :  type-qualifier type-qualifier-list type-qualifier  enum-specifier :  enum identifier opt { enumerator-list } enum identifier  enumerator-list :  enumerator enumerator-list , enumerator  enumerator :  enumeration-constant enumeration-constant = constant-expression  enumeration-constant :  identifier  struct-or-union-specifier :  struct-or-union identifier opt { struct-declaration-list } struct-or-union identifier  struct-or-union :  struct union  struct-declaration-list :  struct-declaration struct-declaration-list struct-declaration  struct-declaration :  specifier-qualifier-list struct-declarator-list ;  specifier-qualifier-list :  type-specifier specifier-qualifier-list opt type-qualifier specifier-qualifier-list opt  struct-declarator-list :  struct-declarator struct-declarator-list , struct-declarator  struct-declarator :  declarator type-specifier declarator opt : constant-expression  parameter-declaration :  declaration-specifiers declarator                /* Named declarator */ declaration-specifiers abstract-declarator opt /* Anonymous declarator */  identifier-list :      /* For old-style declarator */  identifier identifier-list , identifier  abstract-declarator :         /* Used with anonymous declarators */  pointer pointer opt direct-abstract-declarator  direct-abstract-declarator :  ( abstract-declarator ) direct-abstract-declarator opt [ constant-expression opt ] direct-abstract-declarator opt ( parameter-type-list opt )  initializer :  assignment-expression { initializer-list }   /* For aggregate initialization */  { initializer-list , }  initializer-list :  initializer initializer-list , initializer  type-name :  specifier-qualifier-list abstract-declarator opt  typedef-name :  identifier  extended-decl-modifier-seq :/*   Microsoft Specific */  extended-decl-modifier opt extended-decl-modifier-seq extended-decl-modifier  extended-decl-modifier :   /* Microsoft Specific */  thread naked dllimport dllexport

user: user-0
content: 引用 2 楼 zhao4zhong1 的回复:“变量赋值”和“变量初始化”不是一回事！ “变量赋值”发生在运行期，其写法遵循赋值语法规定。 “变量初始化”发生在编译期或运行期，其写法遵循初始化列表语法规定。  有对应的赋值语法和初始化语法说明吗

user: user-3
content: “变量赋值”和“变量初始化”不是一回事！ “变量赋值”发生在运行期，其写法遵循赋值语法规定。 “变量初始化”发生在编译期或运行期，其写法遵循初始化列表语法规定。

user: user-4
content: 对结构体用{}赋值只能在初始化中进行 struct timeval time_start, time_end,ime1, time2, time3, time4, time5 = { 0 }; time_start = time_end = time1 = time2 = time3 = time4 = time5;

user: user-3
content: C Assignment Operators An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but is not an l-value.  Syntax  assignment-expression :  conditional-expression unary-expression assignment-operator assignment-expression  assignment-operator : one of  =   *=   /=   %=   +=   –=   <<=   >>= &=   ^=   |=   The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators:  Operator Operation Performed  =  Simple assignment  *=  Multiplication assignment  /=  Division assignment  %=  Remainder assignment  +=  Addition assignment  –=  Subtraction assignment  <<=  Left-shift assignment  >>=  Right-shift assignment  &=  Bitwise-AND assignment  ^=  Bitwise-exclusive-OR assignment  |=  Bitwise-inclusive-OR assignment    In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place. The left operand must not be an array, a function, or a constant. The specific conversion path, which depends on the two types, is outlined in detail in Type Conversions.

user: user-3
content: Summary of Declarations declaration :  declaration-specifiers attribute-seq opt init-declarator-list opt ;                         /* attribute-seq is Microsoft Specific */  declaration-specifiers :  storage-class-specifier declaration-specifiers opt type-specifier declaration-specifiers opt type-qualifier declaration-specifiers opt  attribute-seq :            /* attribute-seq is Microsoft Specific */  attribute attribute-seq opt  attribute : one of      /* Microsoft Specific */  __asm __fastcall  __based __inline  __cdecl __stdcall    init-declarator-list :  init-declarator init-declarator-list , init-declarator  init-declarator :  declarator declarator = initializer /* For scalar initialization */   storage-class-specifier :  auto register static extern typedef __declspec ( extended-decl-modifier-seq ) /* Microsoft Specific */  type-specifier :  void char short int __int8       /* Microsoft Specific */ __int16      /* Microsoft Specific */ __int32      /* Microsoft Specific */ __int64      /* Microsoft Specific */ long float double signed unsigned struct-or-union-specifier enum-specifier typedef-name  type-qualifier :  const volatile  declarator :  pointer opt direct-declarator  direct-declarator :  identifier ( declarator ) direct-declarator [ constant-expression opt] direct-declarator ( parameter-type-list )   /* New-style declarator */ direct-declarator ( identifier-list opt )      /* Obsolete-style declarator */  pointer :  * type-qualifier-list opt * type-qualifier-list opt pointer  parameter-type-list :                           /* The parameter list */  parameter-list parameter-list , ...  parameter-list :  parameter-declaration parameter-list , parameter-declaration  type-qualifier-list :  type-qualifier type-qualifier-list type-qualifier  enum-specifier :  enum identifier opt { enumerator-list } enum identifier  enumerator-list :  enumerator enumerator-list , enumerator  enumerator :  enumeration-constant enumeration-constant = constant-expression  enumeration-constant :  identifier  struct-or-union-specifier :  struct-or-union identifier opt { struct-declaration-list } struct-or-union identifier  struct-or-union :  struct union  struct-declaration-list :  struct-declaration struct-declaration-list struct-declaration  struct-declaration :  specifier-qualifier-list struct-declarator-list ;  specifier-qualifier-list :  type-specifier specifier-qualifier-list opt type-qualifier specifier-qualifier-list opt  struct-declarator-list :  struct-declarator struct-declarator-list , struct-declarator  struct-declarator :  declarator type-specifier declarator opt : constant-expression  parameter-declaration :  declaration-specifiers declarator                /* Named declarator */ declaration-specifiers abstract-declarator opt /* Anonymous declarator */  identifier-list :      /* For old-style declarator */  identifier identifier-list , identifier  abstract-declarator :         /* Used with anonymous declarators */  pointer pointer opt direct-abstract-declarator  direct-abstract-declarator :  ( abstract-declarator ) direct-abstract-declarator opt [ constant-expression opt ] direct-abstract-declarator opt ( parameter-type-list opt )  initializer :  assignment-expression { initializer-list }   /* For aggregate initialization */ { initializer-list , }  initializer-list :  initializer initializer-list , initializer  type-name :  specifier-qualifier-list abstract-declarator opt  typedef-name :  identifier  extended-decl-modifier-seq :/*   Microsoft Specific */  extended-decl-modifier opt extended-decl-modifier-seq extended-decl-modifier  extended-decl-modifier :   /* Microsoft Specific */  thread naked dllimport dllexport

user: user-3
content: Summary of Declarations declaration :  declaration-specifiers attribute-seq opt init-declarator-list opt ;                         /* attribute-seq is Microsoft Specific */  declaration-specifiers :  storage-class-specifier declaration-specifiers opt type-specifier declaration-specifiers opt type-qualifier declaration-specifiers opt  attribute-seq :            /* attribute-seq is Microsoft Specific */  attribute attribute-seq opt  attribute : one of      /* Microsoft Specific */  __asm __fastcall  __based __inline  __cdecl __stdcall    init-declarator-list :  init-declarator init-declarator-list , init-declarator  init-declarator :  declarator declarator = initializer /* For scalar initialization */  storage-class-specifier :  auto register static extern typedef __declspec ( extended-decl-modifier-seq ) /* Microsoft Specific */  type-specifier :  void char short int __int8       /* Microsoft Specific */ __int16      /* Microsoft Specific */ __int32      /* Microsoft Specific */ __int64      /* Microsoft Specific */ long float double signed unsigned struct-or-union-specifier enum-specifier typedef-name  type-qualifier :  const volatile  declarator :  pointer opt direct-declarator  direct-declarator :  identifier ( declarator ) direct-declarator [ constant-expression opt] direct-declarator ( parameter-type-list )   /* New-style declarator */ direct-declarator ( identifier-list opt )      /* Obsolete-style declarator */  pointer :  * type-qualifier-list opt * type-qualifier-list opt pointer  parameter-type-list :                           /* The parameter list */  parameter-list parameter-list , ...  parameter-list :  parameter-declaration parameter-list , parameter-declaration  type-qualifier-list :  type-qualifier type-qualifier-list type-qualifier  enum-specifier :  enum identifier opt { enumerator-list } enum identifier  enumerator-list :  enumerator enumerator-list , enumerator  enumerator :  enumeration-constant enumeration-constant = constant-expression  enumeration-constant :  identifier  struct-or-union-specifier :  struct-or-union identifier opt { struct-declaration-list } struct-or-union identifier  struct-or-union :  struct union  struct-declaration-list :  struct-declaration struct-declaration-list struct-declaration  struct-declaration :  specifier-qualifier-list struct-declarator-list ;  specifier-qualifier-list :  type-specifier specifier-qualifier-list opt type-qualifier specifier-qualifier-list opt  struct-declarator-list :  struct-declarator struct-declarator-list , struct-declarator  struct-declarator :  declarator type-specifier declarator opt : constant-expression  parameter-declaration :  declaration-specifiers declarator                /* Named declarator */ declaration-specifiers abstract-declarator opt /* Anonymous declarator */  identifier-list :      /* For old-style declarator */  identifier identifier-list , identifier  abstract-declarator :         /* Used with anonymous declarators */  pointer pointer opt direct-abstract-declarator  direct-abstract-declarator :  ( abstract-declarator ) direct-abstract-declarator opt [ constant-expression opt ] direct-abstract-declarator opt ( parameter-type-list opt )  initializer :  assignment-expression { initializer-list }   /* For aggregate initialization */  { initializer-list , }  initializer-list :  initializer initializer-list , initializer  type-name :  specifier-qualifier-list abstract-declarator opt  typedef-name :  identifier  extended-decl-modifier-seq :/*   Microsoft Specific */  extended-decl-modifier opt extended-decl-modifier-seq extended-decl-modifier  extended-decl-modifier :   /* Microsoft Specific */  thread naked dllimport dllexport

user: user-0
content: 这个也没有介绍两者具体的语法