Why am I getting "pointer type arithmetic"?

In the following code, I get an error when I write out a float pointer to a struct, but the compiler doesn't complain if I cite something different. Why is he 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 tell me I have * x a pointer to a color, what would be the correct way to get that color? I am currently using Color c = *((Color *)(value))

, but I don't think this is the correct way to do it.

+3


source to share


1 answer


The conversions you do are not conversions from pointers to integers or structures, but conversions from floating point numbers to integers and structures. Expression

(CELbool)*x

      

sets the value *x

(which, as it x

is float*

, is float

) in CELBool

which you define a typedef for some integer type. This conversion is fine as C allows conversions between floating point and integral values, as there is a sane way to convert. However, the second cast

(Color)*x

      



Color

is a structure, and C does not define conversions between floating point and struct

s, just as it does not define conversions between integral and struct

s, since there is no sane way to do this conversion at all.

The reason for the "required pointer or arithmetic type" error is that the cast from float

must be to some type that can be converted to float

, which will be either some real type, integral type, or pointer type, since it float

can be converted to pointers (although it's really a bad idea for that!) The type is Color

not expected in the compilers list of sensible things to put there, hence the error.

Hope this helps!

+7


source







All Articles