2 ms·
int i; float f = *(float*)&i; I'm not saying you should do this, but you can. Or to use an example the author didn't regret: struct foo { int a
by dspeyer 14y ago
int i;
float f = *(float*)&i;
I'm not saying you should do this, but you can.
Or to use an example the author didn't regret:
struct foo {
int a;
int b;
int c;
}__attribute__((packed));
void print(foo* f) {
int *p = f;
int i;
for (i=0; i<sizeof(foo)/sizeof(int); i++) {
printf("%d,",p[i]);
}
}
- emeraldd 14y agoHere's one I used in a GC implementation a while back. That last 'uint_8t obj[]' is used to hold the object that was actually allocated. struct meta_obj { meta_obj_type *next; // next object in our list mark_type mark; size_t size; gc_type_def type_def; uint8_t obj[]; // contained object }; Or another (contrived) example: struct obj_type { obj_type_enum type; }; struct string_obj_type { obj_type_enum type; char *c; }; You start with a collection of obj_type pointers and cast them to the appropriate pointer type when you have identified the actual contained struct. Useful if you need to have a heterogeneous list of things.