LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2014, 11:09 AM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Rep: Reputation: 10
assigning variable to a pointer w/ a type cast


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;
 
Old 08-16-2014, 12:10 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Possible, but makes no sense, causes segmentaion fault. Why would you do that?
Code:
printf("p is %p\n, p);     //memory location
printf("y is at %p\n, &y);     //memory location

return 0;

Last edited by NevemTeve; 08-16-2014 at 12:12 PM.
 
Old 08-16-2014, 12:26 PM   #3
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
According to your code, p is a pointer to a floating variable who's memory location is 120. No wonder you are getting seg faults.

One way to achieve what you want is
Code:
p = malloc(sizeof(float));
*p = (float) y;
 
Old 08-16-2014, 04:34 PM   #4
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
Code:
p = malloc(sizeof(float));
*p = (float) y;
This doesn't compile
How do you assign to a pointer of type float from a non-pointer variable of type int.
 
Old 08-16-2014, 05:49 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by doughyi8u View Post
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.
 
Old 08-16-2014, 05:52 PM   #6
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
I'm trying to *fully* understand pointers
 
Old 08-16-2014, 09:48 PM   #7
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by doughyi8u View Post
This doesn't compile
How do you assign to a pointer of type float from a non-pointer variable of type int.
Hmmm, it compiles just fine when I do it. Why don't you show us what you wrote? There is bound to be some typos there.

p = malloc(sizeof(float)); reserves enough memory in the heap to store a floating point number and stores the address of that memory in p.

*p = (float) y; casts (converts) y into a floating point number and stores the result in the memory location pointed to by p.
 
Old 08-17-2014, 12:00 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> This doesn't compile

Why don't you try it? If tried, why don't you quote the error message?
 
Old 08-17-2014, 12:37 AM   #9
mhogomchungu
LQ Newbie
 
Registered: Mar 2014
Posts: 20

Rep: Reputation: Disabled
Quote:
Originally Posted by doughyi8u View Post
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.
 
Old 08-17-2014, 12:52 AM   #10
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by mhogomchungu View Post
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.

Last edited by psionl0; 08-17-2014 at 12:55 AM.
 
Old 08-17-2014, 01:13 AM   #11
mhogomchungu
LQ Newbie
 
Registered: Mar 2014
Posts: 20

Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
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.
 
Old 08-17-2014, 01:37 AM   #12
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by mhogomchungu View Post
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.

Last edited by psionl0; 08-17-2014 at 01:38 AM.
 
Old 08-17-2014, 02:12 AM   #13
mhogomchungu
LQ Newbie
 
Registered: Mar 2014
Posts: 20

Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
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]

[1] http://cellperformance.beyond3d.com/...-aliasing.html

Last edited by mhogomchungu; 08-17-2014 at 02:33 AM.
 
Old 08-17-2014, 02:26 AM   #14
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Casting from void* is still legal. In fact, it is necessary if you want to do anything with the data.
 
Old 08-17-2014, 09:55 AM   #15
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
Code:
#include <stdio.h>
#include <stdlib.h>

int
main()
{
    float *p;
    //void *p;
    int y = 120;
    //void *x;

    //x = &y;
    p = malloc(sizeof(float));
    *p = (float)y;
    printf("p is %f\n", *p);

    return 0;
}
compiling this gives the following error:
test1.c:13:26: error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ pointer cast from object to function PatrickNew Programming 27 08-25-2008 01:30 PM
pointer cast c++ santana Programming 10 10-19-2007 07:55 AM
makes pointer from integer without a cast ? hubabuba Programming 2 01-28-2005 05:28 PM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration