Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

question about cast

 
 
Yacine
Guest
Posts: n/a
 
      01-15-2004
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

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      01-15-2004
Yacine <> 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.

Richard
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      01-15-2004
Yacine <> spoke thus:

> void main(){


Hold it right there. Sounds like you could use

http://www.eskimo.com/~scs/C-faq/top.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Yacine
Guest
Posts: n/a
 
      01-15-2004
Richard Bos wrote:
> Yacine <> 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.
>
> Richard

The return type of main has nothing to do with the problem I'm talking
about. My question is: is it possible to cast a pointer to a double ? I
think it does not make more sense to cast a pointer to int than casting
a pointer to double ...

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-15-2004
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.

--
pete
 
Reply With Quote
 
Yacine
Guest
Posts: n/a
 
      01-15-2004
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 ?

 
Reply With Quote
 
pete
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 ?


If you have made sure that there are no alignment problems
and no size problems, then you can

*(int *)&y = x;

If you haven't made sure, then don't do that.

--
pete
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-15-2004
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 :
"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
 
pete
Guest
Posts: n/a
 
      01-15-2004
pete wrote:
>
> 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 ?

>
> If you have made sure that there are no alignment problems
> and no size problems, then you can
>
> *(int *)&y = x;


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

Excuse me, I forgot how invloved it was.

> If you haven't made sure, then don't do that.


--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-15-2004
pete wrote:
>
> pete wrote:
> >
> > 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 ?

> >
> > If you have made sure that there are no alignment problems
> > and no size problems, then you can
> >
> > *(int *)&y = x;

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


You also have to make sure that (int)x is an integer value.

--
pete
 
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