4 ms·
"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...
by pimentel 14y ago
"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...
- kscaldef 14y agoC is weakly-typed. You can cast anything to anything else and the compiler won't stop you.
- emeraldd 14y ago> Wouldn't that imply that a variable of one type could be coerced into another type? Yup! It's quite common to cast things from a void * to a more specific one. You do it every time you call malloc.
- dspeyer 14y agoint 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.