If I have a char* which I know contains data already and want to interpret this data as a struct, how can I safely do this?
E.g if I have
Code:
struct s
{
int i;
float f;
short s;
};
char* c;
...
// c points to valid data now
I know you can't do
Code:
struct s* data = (struct s*)c;
as this violates strict aliasing rules so I was wondering if the safest way would be to use memcpy assuming the struct is packed?
Code:
struct s data;
memcpy(&data, c, sizeof(struct s));
Also is casting to first then the struct allowed?
Code:
struct s* data = (struct s*)(void*)c;
Thanks