memcpy a void pointer to a union
union foo
{
char c;
int i;
}
void func(void * src)
{
union foo dest;
memcpy(&dest, src, sizeof(union foo)); //here
}
If I call func like this:
int main()
{
char c;
int i;
func(&c);
func(&i);
return 0;
}
In the call func(&c), the size of c is less than sizeof(union foo), which
may be dangerous, right?
Is the line with memcpy correct? And if not, how to fix it?
No comments:
Post a Comment