Assuming we dereference correctly and use the integer right, is this a good programming practice?
union {
int x;
struct node * next;
};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unions are rather low-level language feature. There are no meaningful reason for any restrictive “good programming practices” to exist with regards to which types can reside together in a union. The purpose of union is to “multiplex” memory usage: to save space by storing several unrelated objects with non-overlapping lifetimes in the same memory region.
If that’s what you need – go for it.
Unions are sometimes used for raw memory reinterpretation (AKA type-punning). This usage of unions used to be illegal, until it was formally legalized in one of the late technical corrigendums to C99 standard. While it has its uses, type-punning can indeed be seen as questionable programming practice.