将结构设置为等于值

时间:2012-11-05 19:24:32

标签: c struct

假设我的struct字段x也是struct(不是指向struct的指针)。

如果我说object.x = 0,实际发生了什么?

3 个答案:

答案 0 :(得分:2)

我认为它不会编译但我现在无法检查但是

object.x = {0};

应将所有字段初始化为0。

更新

之前没有编译,因为这只能在声明时完成,但是下面的确定

object.X = (struct struct1) {0};

相当于

{
  struct struct1 temp = {0};
  object.X = temp;
}

答案 1 :(得分:2)

您将收到编译错误。

error: incompatible types in assignment

您不能将int分配给结构变量。

答案 2 :(得分:1)

它无效。您会收到不兼容类型的错误,例如:

incompatible types when assigning to type ‘struct X’ from type ‘int’.