Amit Singh Tomar
Amit Singh Tomar

Reputation: 8610

Can a Union be self-referenced in C?

I have been asked in an interview "Can a Union be self-referenced?"

I know that struct can self-reference, but I'm really confused about union. I've read in one text book that union can self-reference, but it didn't say anything more on the subject.

Can anyone confirm whether a union is or is not able to be self-referenced?

Upvotes: 5

Views: 2176

Answers (1)

Jens Gustedt
Jens Gustedt

Reputation: 78903

sure it can, really the same way as struct:

union toto {
  union toto* a;
  unsigned b;
};

as soon as the tag identifier toto is known to be a union type union toto* is a pointer to an incomplete type.

Difficult to figure out that this knowledge will serve you for something else than an interview, though.

Upvotes: 4

Related Questions