473,902 Members | 5,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about cast

Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well, and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?
Why is it not allowed to cast a pointer to a double in the seconde case ?
Thanks
Yacine

Nov 14 '05
28 10288
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message
news:bu******** **@hercules.bti nternet.com...
Yacine wrote:
Does anyone know why

void main()
int main(void)

{
double x;
double *y;
(int) y = x;
}

compiles well,


You can't assign to a value, only to an object. (int)y is a value, not an
object. You should get a diagnostic. Turn up your warning level.
and why

void main()


int main(void)

{
double x;
double *y;
(double) y = x;
}

does not compile well ?!?


Same reason the first one shouldn't. You can't assign to a value, only to

an object. (double)y is a value, not an object.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


My question may not be related to OP much.
What is an object that is different from value in CO?
Could you elaborate further on "You can't assign to a value, only to an
object."
Thanks!
Nov 14 '05 #21
On 15 Jan 2004 21:45:07 GMT, Chris Torek <no****@torek.n et> wrote:
On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <ya**@netcourri er.com>
wrote:

[snippage]
double x;
double *y;
(int) y = x;


In article <9p************ *************** *****@4ax.com>
Horst Kraemer <ho***********@ epost.de> writes:
Your compiler uses some extension to the C language.


This much is reasonably clear (the other "likely" possibility is that
his compiler is simply broken :-) ).
In C the first assignment may be written as

*(int*)&y = x;


Note that one popular compiler that adds a "cast as lvalue" rule,
the GNU C Compiler (gcc), defines cast-as-lvalue *very* differently.
The assignment to (int)y is not equivalent to the above, but rather
to the (syntactically valid but semantically dodgy) ANSI/ISO C code:

(int)(y = (double *)(int)&x)


Apparently it doesn't act according the definition:

For this file

double x=1;
double *y=0;

void f(void)
{
(int)y=x;
}
gcc 3.2 (mingw) produces this output
.file "foo.c"
.globl _x
.data
.align 8
_x:
.long 0
.long 1072693248
.globl _y
.align 4
_y:
.long 0
.text
.align 2
.globl _f
.def _f; .scl 2; .type 32; .endef
_f:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
fldl _x ## load double from _x
fnstcw -2(%ebp)
movw -2(%ebp), %ax
movb $12, %ah
movw %ax, -4(%ebp)
fldcw -4(%ebp)
fistpl _y ## store int to _y
fldcw -2(%ebp)
leave
ret
which converts the double value in &x to int and stores it to &y and
is semantically equivalent to

*(int*)&y = x;

Regards
Horst

Nov 14 '05 #22
"Garma" <Ga*****@nosyss erv.com> writes:
[...]
My question may not be related to OP much.
What is an object that is different from value in CO?
Could you elaborate further on "You can't assign to a value, only to an
object."


(Is "CO" a typo for "C"?)

An object is a variable (more or less; the C standard rarely uses the
term "variable", and an object needn't be modifiable). An object is a
container; the thing it contains is a value.

For example:

int x;
x = 42; /* ok: x is an object */
42 = x; /* illegal: 42 is a value, so you can't assign to it */
(x + 1) = 43; /* illegal: x + 1 is a value, so you can't assign to it */

Look up "lvalue" in your C textbook.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #23
>On 15 Jan 2004 21:45:07 GMT, Chris Torek <no****@torek.n et> wrote:
Note that one popular compiler that adds a "cast as lvalue" rule,
the GNU C Compiler (gcc), defines cast-as-lvalue *very* differently.
The assignment to (int)y is not equivalent to the above, but rather
to the (syntactically valid but semantically dodgy) ANSI/ISO C code:

(int)(y = (double *)(int)&x)

In article <fk************ *************** *****@4ax.com>
Horst Kraemer <ho***********@ epost.de> writes:Apparently it doesn't ...
Sorry, I goofed -- I added an "&" that was not in the original
code, of which this is a version:
double x=1;
double *y=0;

void f(void)
{
(int)y=x;
}
Since y has type "double *", the "GCC meaning" is:

(int)(y = (double *)(int)x)

If you compile this:
*(int*)&y = x;


you do indeed get the same machine code, because "int", "int *",
and "double *" all have the same size and format on the x86; and
of course the last quoted C code line above has the same meaning
as:

*(int *)&y = (int)x;

Converting x to int requires code that changes the representation
-- this is the fldl/fistpl instruction pair (the control word store
and reload, fnstcw and fldcw, are red herrings) -- but converting
the resulting "int" to "double *" requires no code on the (32-bit)
Intel (it does require code on IA-64 and the like). This hides
the difference between "then convert int to double *" vs "now just
assign". If you have access to a 64-bit machine, gcc should generate
different code for these two cases (i.e., the two casts then assign
to y, vs the one cast or automatic conversion to assign to *(int *)&y).

(You will also get warnings about converting from 32-bit integers
to 64-bit pointers, where that occurs.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #24
Yacine <ya**@netcourri er.com> wrote:
Richard Bos wrote:
Yacine <ya**@netcourri er.com> wrote:

Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well,
Because your implementation is broken, or you're calling it the wrong
way. main() returns int. The cast and subsequent assignment are about as
dubious as Bush Junior's veracity, btw.

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?


Because casting a pointer to int is dubious, but possible, while casting
a pointer to double makes no sense at all.

The return type of main has nothing to do with the problem I'm talking
about.
You do not know that. Undefined is undefined.
My question is: is it possible to cast a pointer to a double ?
And the obvious answer is "no". As you'd already observed yourself.
I think it does not make more sense to cast a pointer to int than casting
a pointer to double ...


The Standard Committee thought differently, and so do I.

To every address in memory, you can at least assign _some_ kind of index
number. That index number might not make sense in the context of any
other program running on the same computer, or indeed when interpreted
as any other kind of pointer than the original, but it is, at least,
possible, and might in principle be useful to someone doing a very
low-level, system-specific job.
Transforming an address in memory to a floating-point number, however,
does not make sense at all, not even in Gnuck. How, to begin with, would
you map them?

Richard
Nov 14 '05 #25
Horst Kraemer <ho***********@ epost.de> wrote:
Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==si zeof y on this platform and it refuses
(double)y=x because sizeof(double)> sizeof y on this platform.


This does not explain why we _can_ assign longs to ints and vice versa.

Richard
Nov 14 '05 #26
On 16 Jan 2004 02:22:15 GMT, Chris Torek <no****@torek.n et> wrote:
On 15 Jan 2004 21:45:07 GMT, Chris Torek <no****@torek.n et> wrote:
Note that one popular compiler that adds a "cast as lvalue" rule,
the GNU C Compiler (gcc), defines cast-as-lvalue *very* differently.
The assignment to (int)y is not equivalent to the above, but rather
to the (syntactically valid but semantically dodgy) ANSI/ISO C code:

(int)(y = (double *)(int)&x)


In article <fk************ *************** *****@4ax.com>
Horst Kraemer <ho***********@ epost.de> writes:
Apparently it doesn't ...


Sorry, I goofed -- I added an "&" that was not in the original
code, of which this is a version:
double x=1;
double *y=0;

void f(void)
{
(int)y=x;
}


Since y has type "double *", the "GCC meaning" is:

(int)(y = (double *)(int)x)


OK, the fog clears up. Thanks for your input.

--
Horst

Nov 14 '05 #27
In <bu************ @ID-217261.news.uni-berlin.de> Yacine <ya**@netcourri er.com> writes:
about. My question is: is it possible to cast a pointer to a double ?
No, the language does not define such a conversion.
I think it does not make more sense to cast a pointer to int than casting
a pointer to double ...


You're dead wrong. On most implementations using linear addressing,
pointers contain memory addresses which are integers and converting
a pointer to an integer is a run time no-op. Such conversions are often
useful, e.g. when you need to check if a pointer is correctly aligned.

OTOH, converting an address to a floating point type doesn't have much
meaning or use. It's still possible, though, just not directly:
(double)(unsign ed long)pointer.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #28
"Trollsdale " wrote:
Yacine wrote:

<snip>
Thanks, that the answer I was waiting for.


Sorry about your encounters with our "indigenous trolls".
After awhile you'll begin to recognize them and ignore them.


Whoever bell'd that cat did a good job.
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #29

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

Similar topics

4
547
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question"; (string)xyz; now we only have a type object of System.String type
15
1746
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ?
4
10483
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
26
2144
by: Janice | last post by:
What is the major reason for using void*? When should we use void* for both input arguments and return value? How can we cast the void* pointer to the type we need? Thanx
9
378
by: walt.stoneburner | last post by:
Why does aliasing an interface fail at runtime? SAMPLE WORKING CODE ... IHTMLDocument2 doc = (IHTMLDocument2) browser.Document; ... BROKEN CODE public interface IFooBar : IHTMLDocument2 { } ...
2
1945
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix. I have a problem, however, with /calling/ foo. If I call foo as foo(a,m,n), my compiler (gcc) complains about:
6
365
by: spibou | last post by:
In page 81 of N1124 in footnote 87 we read: If the value of the expression is represented with greater precision or range than required by the type named by the cast (6.3.1.8), then the cast specifies a conversion even if the type of the expression is the same as the named type. Can someone give me an example of what this means ?
14
1930
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
7
1965
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I think it should be the latter because there are 4 parser tokens here, sizeof, int, * and p, the first and the second are of same precedence which are higher than the third and all three operators are
14
2454
by: subramanian100in | last post by:
Suppose fgets is used to read a line of input. char str; fgets(str, sizeof(str), stdin); After reading some characters on the same line, if end-of-file is encountered, will fgets return the 'str' parameter and set EOF indicator for the stream ? Or will it return the string argument and set EOF indicator only on subsequent call ? Kindly clarify
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4725
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.