Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question about cast

Reply
Thread Tools

question about cast

 
 
Horst Kraemer
Guest
Posts: n/a
 
      01-15-2004
On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <(E-Mail Removed)>
wrote:

> Does anyone know why
>
> void main(){
> double x;
> double *y;
> (int) y = x;
> }


This shouldn't compile at all because it isn't C - even if it accepts
"void main()".

In C you can't assign anything to "(int)y" because (int)y is a *value*
and not an object (LValue).

> compiles well, and why
>
> void main(){
> double x;
> double *y;
> (double) y = x;
> }
>
> does not compile well ?!?


Idem.

Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==sizeof y on this platform and it refuses
(double)y=x because sizeof(double)>sizeof y on this platform.

In C the first assignment may be written as

*(int*)&y = x;

and the second as

*(double*)&y = x;

but the behaviour of the second assigment is undefined on your
platform because the type of the left operand is double and sizeof
(double) (probably == > sizeof y (probably ==4). The assigment would
write beyond the memory belonging to y. Moreover a double object may
have different aligment requirements than a

Regards
Horst

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-15-2004
Yacine wrote:

> The return type of main has nothing to do with the problem I'm talking
> about.


If you don't want people to point out errors in your code, don't post
erroneous code.

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"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
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-15-2004
Yacine wrote:

> Does anyone know why
>
> void main(){

^^^^
incorrect return type for main
> double x;
> double *y;
> (int) y = x;

^^^^
illegal use of a cast where an lvalue is required.
> }
>
> compiles well,


"Bullshit," you say, "Bullshit!". The above is hopelessly broken.

> and why
>
> void main(){
> double x;
> double *y;
> (double) y = x;
> }
>
> does not compile well ?!?


For the same reason the first one does *not* "compile well."

> Why is it not allowed to cast a pointer to a double in the seconde case ?


For the same reason that your first example is not allowed.



--
Martin Ambuhl
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-15-2004
Yacine wrote:

> pete wrote:
>
>> Yacine wrote:
>>
>>> Does anyone know why
>>>
>>> void main(){
>>> double x;
>>> double *y;
>>> (int) y = x;
>>> }
>>>
>>> compiles well,

>>
>>
>>
>> Because you are not using a conforming implementation of C.
>> A cast on the left operand of the assignment operator
>> is a constraint violation.
>>

> How can I handle that problem ?
>


Patient: "My arm hurts when I move it like this. What can I do?"
Doctor: "Don't move your arm like that."


--
Martin Ambuhl
 
Reply With Quote
 
Yacine
Guest
Posts: n/a
 
      01-15-2004
Horst Kraemer wrote:

> On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <(E-Mail Removed)>
> wrote:
>
>
>>Does anyone know why
>>
>>void main(){
>> double x;
>> double *y;
>> (int) y = x;
>>}

>
>
> This shouldn't compile at all because it isn't C - even if it accepts
> "void main()".
>
> In C you can't assign anything to "(int)y" because (int)y is a *value*
> and not an object (LValue).
>
>
>>compiles well, and why
>>
>>void main(){
>> double x;
>> double *y;
>> (double) y = x;
>>}
>>
>>does not compile well ?!?

>
>
> Idem.
>
> Your compiler uses some extension to the C language. It allows
> (int)y=x because sizeof(int)==sizeof y on this platform and it refuses
> (double)y=x because sizeof(double)>sizeof y on this platform.
>
> In C the first assignment may be written as
>
> *(int*)&y = x;
>
> and the second as
>
> *(double*)&y = x;
>
> but the behaviour of the second assigment is undefined on your
> platform because the type of the left operand is double and sizeof
> (double) (probably == > sizeof y (probably ==4). The assigment would
> write beyond the memory belonging to y. Moreover a double object may
> have different aligment requirements than a
>
> Regards
> Horst
>

Thanks, that the answer I was waiting for

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-15-2004
Yacine <(E-Mail Removed)> writes:
> pete wrote:
>
> > Yacine wrote:
> >
> >>Does anyone know why
> >>
> >>void main(){
> >> double x;
> >> double *y;
> >> (int) y = x;
> >>}
> >>
> >> compiles well,

> > Because you are not using a conforming implementation of C.
> > A cast on the left operand of the assignment operator
> > is a constraint violation.
> >

> How can I handle that problem ?


Some other languages, perhaps with names starting with 'C' and
containing punctuation characters, may allow a cast as the left
operand of an assignment operator.

You're trying to do something that doesn't make any sense. The
compiler is preventing you from doing it. I don't see a problem.

Seriously, just what are you trying to do?

Here's the code that the compiler complains about:

double x;
double *y;
(double) y = x; /* illegal */

x is a double; y is a pointer to double.

If you want to make y point to x, you can do this:

y = &x;

If you want to make y point to a float variable with the value of x,
without changing the memory location to which y points, you can do this:

*y = x;

but only after initializing y so it points to a valid memory location.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      01-15-2004
>On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <(E-Mail Removed)>
>wrote:

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


In article <(E-Mail Removed)>
Horst Kraemer <(E-Mail Removed)> 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)

>> (double) y = x;

>and the second as
> *(double*)&y = x;


Gcc would define this as:

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

which would then draw a complaint ("pointer value used where a
floating point value was expected").

For more details on gcc's weird sort-of-like-C language GNUC (which
I pronounce as "ganuck", more or less), see "info gcc", assuming
the info files have been installed along with the compiler. The
definition of lvalue-as-cast is under "C Extensions" and then
"Lvalues".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.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.
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-15-2004
Yacine wrote:

> Horst Kraemer 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.



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-15-2004
Horst Kraemer <(E-Mail Removed)> writes:
[...]
> Your compiler uses some extension to the C language. It allows
> (int)y=x because sizeof(int)==sizeof y on this platform and it refuses
> (double)y=x because sizeof(double)>sizeof y on this platform.


I don't believe the sizes of the various types have anything to do
with this.

In C, a cast is not allowed on the left side of an assignment,
regardless of the types. If a cast is being used as a value (not as
an lvalue), it's legal to convert from a pointer type to an integer
type or vice versa, whether the sizes match or not. It's not legal to
convert between pointer types and floating-point types.

I would guess that the extension implemented by whatever compiler the
OP is using (allowing a cast as an lvalue) would following the same
rules. (As I vaguely alluded to earlier, I think C++ allows casts as
lvalues. I don't know the details, which aren't topical anyway.)

Note that conversions between integers and pointers, though they're
legal, are not usually meaningful. You can convert a pointer to a
sufficiently large integer type (if there is one) and back again and
get the same pointer value. Anything else will probably give you
garbage.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-15-2004
pete <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> pete wrote:
> > *(int *)&y = x;

>
> *(int *)&y = (int)x;
>
> Excuse me, I forgot how invloved it was.


? The (int) cast is redundant!

'* (int *) &y' forms an lvalue of type int, so the expression x will
be converted to an int on assignment.

--
Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
Question: Invalid Cast Exception Error VB Programmer ASP .Net 4 10-28-2003 03:13 PM



Advertisments