Is it possible to rename the variables of a struct when using typedef?
typedef struct {
float x;
float y;
float z;
} Vector;
typedef Vector {
float r;
float g;
float b;
} Color;
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.
Presumably you mean some sort of
typedefthat would let you say: “a Color is the same as a Vector, but with the namesx,yandzchanged tor,gandb, respectively”?If so, the answer is no, C doesn’t provide that (nor can I think of anything it provides that would be a reasonably close substitute).
Edit: you can, however, have a function operate on either one pretty easily:
C guarantees that the initial sequence of items with the same types in the same order (which, in this case is all of them) can be worked with via either selector equally. In other words, if you put a
Vectorinto yourcvand pass it to a function, the function can work with those three items as eitherr,gandbor asx,yandzwithout any problem (e.g., if it writes tog, that will changey, if you view it as aVectorinstead of aColor).Edit: C99, §6.5.2.3/5:
In this case, all the members of
candvhave the same types in the same order, so this rule applies to the entirety of either member. Rereading things, however, this only allows you to “inspect” the common initial sequence, which probably doesn’t include writing to it (though I find it hard to imagine how you’d make reading from any member work without making writing work as well).