"C is a weak, statically typed language"
Wouldn't that imply that a variable of one type could be coerced into another type?
I would say C is strong typed...
Wouldn't that imply that a variable of one type could be coerced into another type?
I would say C is strong typed...
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]);
}
} 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.Yup! It's quite common to cast things from a void * to a more specific one. You do it every time you call malloc.