question about cast

Discussion in 'C Programming' started by Yacine, Jan 15, 2004.

  1. Yacine

    Yacine Guest

    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
     
    Yacine, Jan 15, 2004
    #1
    1. Advertising

  2. Yacine

    Richard Bos Guest

    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
     
    Richard Bos, Jan 15, 2004
    #2
    1. Advertising

  3. 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.
     
    Christopher Benson-Manica, Jan 15, 2004
    #3
  4. Yacine

    Yacine Guest

    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 ...
     
    Yacine, Jan 15, 2004
    #4
  5. Yacine

    pete Guest

    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
     
    pete, Jan 15, 2004
    #5
  6. Yacine

    Yacine Guest

    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 ?
     
    Yacine, Jan 15, 2004
    #6
  7. Yacine

    pete Guest

    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
     
    pete, Jan 15, 2004
    #7
  8. 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
     
    Richard Heathfield, Jan 15, 2004
    #8
  9. Yacine

    pete Guest

    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
     
    pete, Jan 15, 2004
    #9
  10. Yacine

    pete Guest

    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
     
    pete, Jan 15, 2004
    #10
  11. On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <>
    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 ==8) > 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
     
    Horst Kraemer, Jan 15, 2004
    #11
  12. 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 :
    "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
     
    Richard Heathfield, Jan 15, 2004
    #12
  13. 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
     
    Martin Ambuhl, Jan 15, 2004
    #13
  14. 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
     
    Martin Ambuhl, Jan 15, 2004
    #14
  15. Yacine

    Yacine Guest

    Horst Kraemer wrote:

    > On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <>
    > 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 ==8) > 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
     
    Yacine, Jan 15, 2004
    #15
  16. Yacine <> 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) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Jan 15, 2004
    #16
  17. Yacine

    Chris Torek Guest

    >On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <>
    >wrote:

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


    In article <>
    Horst Kraemer <> 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 (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.
     
    Chris Torek, Jan 15, 2004
    #17
  18. 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.
     
    E. Robert Tisdale, Jan 15, 2004
    #18
  19. Horst Kraemer <> 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) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Jan 15, 2004
    #19
  20. pete <> wrote in message news:<>...
    > 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
     
    Peter Nilsson, Jan 15, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Invalid Cast Exception Error

    VB Programmer, Oct 28, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,371
    VB Programmer
    Oct 28, 2003
  2. Christopher Benson-Manica

    Quick cast style question

    Christopher Benson-Manica, Apr 30, 2004, in forum: C++
    Replies:
    15
    Views:
    537
    Jake Montgomery
    May 4, 2004
  3. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,096
    Dan Pop
    Feb 10, 2004
  4. EvilRix
    Replies:
    8
    Views:
    662
    Martin Dickopp
    Feb 14, 2004
  5. Pavel
    Replies:
    7
    Views:
    546
    Pavel
    Sep 19, 2010
Loading...

Share This Page