
-----------------------------------
Srlancelot39
Fri Jul 11, 2014 10:08 am

&quot;a value of type &quot;float&quot; cannot be assigned to an entity of type &quot;float *&quot;&quot;
-----------------------------------
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?

-----------------------------------
DemonWasp
Fri Jul 11, 2014 10:35 am

RE:&quot;a value of type &quot;float&quot; cannot be assigned to an entity of type &quot;float *&quot;&quot;
-----------------------------------
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:
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:

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).

[code]
int *i;
i = (int*) malloc ( sizeof(int) );
*i = 5; // follow the address of pointer (i) and assign its value
i = 5; // overwrite the address of pointer (i) -- bad, don't do this!
free ( i );  // if you overwrite the address of the pointer, you won't be able to free it -- very bad!
[/code]

-----------------------------------
Srlancelot39
Fri Jul 11, 2014 8:46 pm

Re: &quot;a value of type &quot;float&quot; cannot be assigned to an entity of type &quot;float *&quot;&quot;
-----------------------------------
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:
//a_header_file.h
#ifndef a_header_file_h
#define a_header_file_h
struct student
{
float Grade, *p_Grade; //Student's grade
};
#endif

//not_the_main_c_file.c
//Beginning of function===
/*
code
*/
float tempgrade = 0;
students

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:
students

to:
*(students

and now it seems to work.
