473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Casting problem ?!

what is stored in "i" variable in this program ?

long i;
float y;
float number;
y = number;
i = * ( long * ) &y;

notice that "number" variable has been initialized ...

Dec 2 '05 #1
6 1014
Stranger wrote:
what is stored in "i" variable in this program ?

long i;
float y;
float number;
y = number;
i = * ( long * ) &y;

notice that "number" variable has been initialized ...


Where is the initialization of "number"? I nothing initialized, and y
assigned a junk value.
Also, please use C++-style casts - they are provided for a reason.
The result would be a junk value in "i".

Dec 2 '05 #2
i *think* he may have meant "note" that "number" has been initialized.
ie, elsewhere in his program that he has not shown.

however, i wouldnt know whats in i... why cant you just do
i = long(y)? or (long)y?

Dec 2 '05 #3
The first N bytes of y.
N=sizeof(long)

The content of the first N bytes depends on how your computer an OS
represents floats in memory.

Stranger wrote:
what is stored in "i" variable in this program ?

long i;
float y;
float number;
y = number;
i = * ( long * ) &y;

notice that "number" variable has been initialized ...

Dec 2 '05 #4
Mark wrote:
i *think* he may have meant "note" that "number" has been initialized.
ie, elsewhere in his program that he has not shown.

however, i wouldnt know whats in i... why cant you just do
i = long(y)? or (long)y?


Perhaps because that was not the author's intent. If s/he wanted to
truncate y to the nearest whole number, s/he would have done just what
you said. If, however, s/he wanted an implementation-dependent,
bit-wise representation of y, then the nasty pointer casts may
accomplish that (depending on the sizes of the various data types).
Using reinterpret_cast would be preferred because it would signal
non-portable code.

Cheers! --M

Dec 2 '05 #5

mlimber wrote:
Mark wrote:
i *think* he may have meant "note" that "number" has been initialized.
ie, elsewhere in his program that he has not shown.

however, i wouldnt know whats in i... why cant you just do
i = long(y)? or (long)y?


Perhaps because that was not the author's intent. If s/he wanted to
truncate y to the nearest whole number, s/he would have done just what
you said. If, however, s/he wanted an implementation-dependent,
bit-wise representation of y, then the nasty pointer casts may
accomplish that (depending on the sizes of the various data types).
Using reinterpret_cast would be preferred because it would signal
non-portable code.


Isn't the OP's code

float y;
float number;
y = number;

undefined behaviour is number is not initialised before being assigned
to y?

Gavin Deane

Dec 2 '05 #6
Carlos Martinez Garcia wrote:
The first N bytes of y.
N=sizeof(long)

The content of the first N bytes depends on how your computer an OS
represents floats in memory.


It might not be even that. There's no guarantee that float* can
be converted to long* with a cast.
Dec 4 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.