[SOLVED] assigning variable to a pointer w/ a type cast
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to do is assign to a pointer variable of type float from an int.
Nobody gets up in the morning and says "today I'm going to assign a float pointer from an int". Presumably you're trying to solve an actual problem, why don't you tell us what it is.
This is confusing me. I dont' even know if it's possible but what I'm trying to do is assign to a pointer variable of type float from an int.
Code:
float *p;
int y = 120;
p = (float*)y;
printf("p is %f\n, *p); /segmentation fault
printf("p is %f\n, p) /memory location?
return 0;
p = (float*)y;
The above line reads the value of y and then casts it to (float *) and then writes the result to p.
It is equivalent to:
p = (float*)120;
and i dont think thats what you meant,you probably meant to do:
p = (float*)&y;
The above reads the memory address of variable y and then casts it (float*) and then writes the result to p.
This is probably what you wanted.Use this and it will not segfault anymore.
I'm not sure about that. Is sizeof(float) guaranteed to be the same as sizeof(int) ?
If not then even if the program doesn't segfault, you could be setting yourself up for some awful bugs.
ETA *p would not be y converted to floating format.
It will not segfault and it will work as expected on a 32 bit system since int and float both take 4 bytes.
As far as i know:
float always takes 4 bytes.
int is guaranteed to take atleast 2 bytes but it usually take 4 bytes in most modern 32 and 64 bit systems and 8 bytes in some 64 bit systems.
It modern times and on general purpose systems,its safe to assume int takes atleast 4 bytes.
In a 32 bit system where float take 4 bytes and int take 4 bytes,the program will work as expected.
In a system where the width of the two types are different,*p will give different results but will not cause a crash
because *p will read within the width of variable y unless int takes 2 bytes and i doubt anybody has such a system anymore.
Last edited by mhogomchungu; 08-17-2014 at 01:17 AM.
It will not segfault and it will work as expected on a 32 bit system since int and float both take 4 bytes.
Even if they are both 4 bytes, the program will not work as expected - at least, not if you are expecting p to point to a floating representation of the number 120.
Sometimes, one might cast a pointer to a particular data type into a character array if the objective was to manipulate the data bit by bit but I can't think of any valid reason for casting an integer pointer to a float pointer.
Even if they are both 4 bytes, the program will not work as expected - at least, not if you are expecting p to point to a floating representation of the number 120.
correct,it will not work as expected since the bit pattern of 120 will be different in an variable of type int and a variable of type float.
Lessons to be learned for the OP.
1. Casting is bad,dont do it and if you must,do it while feeling ashamed of yourself.
2. Getting a handle to an object of one type through a pointer type of another object is a bad idea and de referencing the handle is actually illegal,this link could be a good start in finding out why its illegal[1]
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.