In the following code, I get an error when I typecast the float pointer to a struct, but the compiler does not complain if I typecast to something else. Why is it doing this?
typedef unsigned byte CELbool;
typedef struct {(...)} Color;
typedef struct {
(...)
Color color;
CELbool b;
} Light;
Light _light;
void function(float *x) {
_light.b = (CELbool)*x; // No error
_light.color = (Color)*x; // (!) Used type 'Color' where arithmetic or pointer type is required
}
Edit: So say that I have a *x is a pointer to a Color, then what would be the proper way to get that Color? I am currently using Color c = *((Color *)(value)), but I don’t think that that is the correct way to do it.
The conversions you are doing are not conversions from pointers into integers or structs, but conversions from floats to integers and structs. The expression
casts the value of
*x(which, sincexis afloat*, is afloat) into aCELBool, which you’ve defined as a typedef for some integral type. This conversion is okay, as C allows for conversions between floating-point and integral values, since there is a reasonable way of doing the conversion. However, the second cast isColoris a struct, and C does not define conversions between floating-point types andstructs, just as it doesn’t define conversions between integral types andstructs, since in general there is no reasonable way to do this conversion.The reason for the error about “pointer or arithmetic type required” is because the cast from the
floatwould need to be to some type thatfloats can be converted to, which would be either some real-valued type, an integral type, or a pointer type, sincefloats can be converted to pointers (though it’s a really bad idea to do so!) The typeColorisn’t expected by the compiler’s list of reasonable things to put there, hence the error.Hope this helps!