jamesatha
jamesatha

Reputation: 7600

Setting a struct equal to a value

Let's say I have a struct with a field x that is also a struct (not a pointer to a struct).

If I say object.x = 0, what is actually happening under the hood?

Upvotes: 2

Views: 740

Answers (3)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

I think it won't compile but i can't check at the moment but

object.x = {0};

Should initialize all fields to 0.

UPDATE

previous doesn't compile because this can be only done at declaration, but following is ok

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

which is equivalent to

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

Upvotes: 2

Jack
Jack

Reputation: 16724

It will not work. You'II get an error of incompatible types, like this:

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

Upvotes: 1

DrummerB
DrummerB

Reputation: 40211

You'll get a compile error.

error: incompatible types in assignment

You can't assign an int to a struct variable.

Upvotes: 2

Related Questions