Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Union be self-referenced in C?

Tags:

c

unions

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?

like image 912
Amit Singh Tomar Avatar asked Oct 10 '22 12:10

Amit Singh Tomar


1 Answers

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.

like image 90
Jens Gustedt Avatar answered Oct 14 '22 04:10

Jens Gustedt