Casting problem ?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stranger

    #1

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

  • Neelesh Bodas

    #2
    Re: Casting problem ?!

    Stranger wrote:[color=blue]
    > 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 ...[/color]

    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".

    Comment

    • Mark

      #3
      Re: Casting problem ?!

      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?

      Comment

      • Carlos Martinez Garcia

        #4
        Re: Casting problem ?!

        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:[color=blue]
        > 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 ...
        >[/color]

        Comment

        • mlimber

          #5
          Re: Casting problem ?!

          Mark wrote:[color=blue]
          > 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?[/color]

          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_cas t would be preferred because it would signal
          non-portable code.

          Cheers! --M

          Comment

          • deane_gavin@hotmail.com

            #6
            Re: Casting problem ?!


            mlimber wrote:[color=blue]
            > Mark wrote:[color=green]
            > > 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?[/color]
            >
            > 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_cas t would be preferred because it would signal
            > non-portable code.[/color]

            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

            Comment

            • Ron Natalie

              #7
              Re: Casting problem ?!

              Carlos Martinez Garcia wrote:[color=blue]
              > 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.
              >[/color]

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

              Comment

              Working...