I have a
structure {
int a;
char b;
} st;
Is there a way to typecast the structure member st.a?
Because in few places I want it as int and in few places I want it as Char*
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.
I suggest to use a union:
Under the assumption that
sizeof(int) == sizeof(char*)holds, you can access the same value in memory by either usingst.u.aorst.u.ptr.For what it’s worth, consider using
size_tinstead ofintas the type of theafield. That way, your code will still be correct in 64bit builds (in which anintmay still be 32bit but a pointer is 64bit).