C99 - in particular, section 6.2.6.1, clause 4 - indicates that copying the representation of an object into an unsigned array of char is allowed:
struct { int foo; double bar; } baz; unsigned char bytes[sizeof baz];
My question is: can we avoid unnecessary memory allocation and copy operations with simple casting? For example:
struct { int foo; double bar; } baz; unsigned char *bytes = (void *)&baz;
Of course, the size will need to be tracked, but is it legal first of all, or does it fall into the scope of the implementation defined by the implementation or undefined?
I ask because I implement an algorithm similar to qsort , and I would like it to work for any array, regardless of type, as qsort does.
c void-pointers opaque-pointers
Chrono kitsune
source share