casting pointers

This is a discussion on casting pointers within the C Programming forums, part of the General Programming Boards category; hi,
i have:
Code:
struct test{
int a;
int b;
char c[20];
};
struct test *ptest;
is it now safe ...

"Safe" depends on your definition. Yes, you're allowed to access the individual bytes in a struct using the mechanism you're using. In fact, if you look up fread() and fwrite() you'll find it allows you to output data in this way (if you use appropriate casts). What is less safe is any interpretation of the meaning of those bytes: the layout of data in structures is often operating system and compiler dependent.

These two bits of code will interoperate correctly if built using the same compiler (and same compiler settings) on the same operating system. They will not necessarily interoperate if the code is built using different compiler (or different compiler settings) or on a different operating system. The reasons are that;

1) sizeof (struct test) is implementation dependent;
2) layout of individual members in a struct is implementation dependent.