Computer Science Canada "a value of type "float" cannot be assigned to an entity of type "float *"" |
Author: | Srlancelot39 [ Fri Jul 11, 2014 10:08 am ] |
Post subject: | "a value of type "float" cannot be assigned to an entity of type "float *"" |
Why is it that a value of type "int" can be assigned to an entity of type "int *", but a value of type "float" cannot be assigned to an entity of type "float *"? My situation is, I am passing an integer to a pointer to an integer and a float to a pointer to a float. It has no problem with the integers, but it doesn't seem to like the same operation performed on floats. Why is this? |
Author: | DemonWasp [ Fri Jul 11, 2014 10:35 am ] | ||||||
Post subject: | RE:"a value of type "float" cannot be assigned to an entity of type "float *"" | ||||||
Without seeing the code, the most likely answer is that C's type system is very loose. A pointer (to anything) is effectively just an integral value (usually 4 or 8 bytes, but could be anything) representing a memory address that you can read to find the thing it points to. So an int* is an integral value that represents a memory address that contains an integral value. A float* is an integral value that represents a memory address that contains a floating-point value. I'm assuming your code looks like this:
The compile error happens because you are trying to set the (integral) pointer address to a floating-point value, which doesn't make any sense. What you really want is to set the pointer's value. To do that, you need to follow, or "dereference" the pointer:
The reason that the integer assignment doesn't fail at compile time is that you are setting the integral pointer address to an integer value, which C allows. If you tried to dereference that pointer then you would (probably) either find garbage data or trigger a segmentation fault (EAccessViolation on Windows).
|
Author: | Srlancelot39 [ Fri Jul 11, 2014 8:46 pm ] | ||||||||
Post subject: | Re: "a value of type "float" cannot be assigned to an entity of type "float *"" | ||||||||
Thank you for that! Yeah, I asked a friend about it today and they too noted that it was attempting to assign a floating point value as an address, which of course does not make sense as you said. Sorry for not elaborating, the code in question looks like this:
I am creating a pointer for Grade (and other variables) so that its value may be updated at its location outside of the function. Otherwise, the value is lost once the function terminates, as you probably know. EDIT: I have changed:
to:
and now it seems to work. |