Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 "a value of type "float" cannot be assigned to an entity of type "float *""
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Srlancelot39




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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:
code:

int *i;
i = 5; // seems to work, but really doesn't do what you expect

float *f;
f = 1.2f;  // compile error


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:

code:

float *f = (float*) malloc ( sizeof ( float ) );  // don't forget to allocate your pointers
*f = 1.2f;  // note the asterisk: that's the dereference operator in this context
free ( f );  // if you malloc() then you MUST free()


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).

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!
Srlancelot39




PostPosted: 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:
c:
//a_header_file.h
#ifndef a_header_file_h
#define a_header_file_h
struct student
{
float Grade, *p_Grade; //Student's grade
};
#endif

c:
//not_the_main_c_file.c
//Beginning of function===
/*
code
*/

float tempgrade = 0;
students[studentcount].p_Grade = &(students[studentcount].Grade);
/*
tempgrade is given a new value here by the user
*/

students[studentcount].p_Grade = tempgrade; //the error is here (at the "=")
/*
code
*/

//End of function===


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:
c:
students[studentcount].p_Grade = tempgrade;


to:
c:
*(students[studentcount].p_Grade) = tempgrade;


and now it seems to work.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: