Casting Pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sidhomsam
    New Member
    • Sep 2007
    • 5

    Casting Pointers

    Question:

    Is it possible to have a portion of the pointers within an Array of float pointers point to an int?

    OR... is it possible to have a float pointer point to an int variable (possibly by typecasting the int to a float when initializing the "pointer / variable" association ?



    Background / need:

    I am writing a communication protocol (Johnson Control's N2) and it requires that I transmit a couple of int values as floats.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    You are aware that doing so would lose the precision of the 'float', correct?

    You might consider looking at the structure of a float and breaking that down into two ints, so you could convert back and forth without losing that precision...

    If you're not worried about precision, why not just put them into ints in the first place?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      A float variable can hold an integer value, so you only need float pointers, I think.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Ganon11
        A float variable can hold an integer value, so you only need float pointers, I think.
        Nitpick: a float variable can hold quite small int values; on most systems both
        ints and floats take four bytes. A float has to store its exponent in there as well
        so it can't store every possible mantissa ("significan t") that can be stored in a
        simple int. Typically a float can store an int x where |x| < 2^24.

        kind regards,

        Jos

        Comment

        • sidhomsam
          New Member
          • Sep 2007
          • 5

          #5
          Here is what I've tried:
          [code=c]
          void sub_example(voi d);

          int int_variable;
          float *float_pointer;

          main()
          {

          int_variable = 4;

          float_pointer = (float*)&int_va riable;

          sub_example();

          printf("%3.3f", *float_pointer) ;

          }

          void sub_example(voi d)
          {
          int_variable = 12;
          }[/code]

          But the output gives me a "0.000"

          I was expecting / hoping for a "12.000"
          Last edited by sicarie; Sep 11 '07, 04:43 PM. Reason: Code tags

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by sidhomsam
            Here is what I've tried:
            [code=c]
            void sub_example(voi d);

            int int_variable;
            float *float_pointer;

            main()
            {

            int_variable = 4;

            float_pointer = (float*)&int_va riable;

            sub_example();

            printf("%3.3f", *float_pointer) ;

            }

            void sub_example(voi d)
            {
            int_variable = 12;
            }[/code]

            But the output gives me a "0.000"

            I was expecting / hoping for a "12.000"
            This has nothing to do with ints vs floats. Study the C/C++ language a bit more
            before you attempt to do tricky numerical conversion things.

            btw, the bit pattern for an int value 4 doesn't yield a float value of 4 ...

            kind regards,

            Jos

            Comment

            • kreagan
              New Member
              • Aug 2007
              • 153

              #7
              I found this article while trying out your problem.

              Void Pointers

              Hopefully this is exactly what you needed. Plus it shows you how to use function pointers! Yummy!

              Comment

              • sidhomsam
                New Member
                • Sep 2007
                • 5

                #8
                Originally posted by JosAH
                This has nothing to do with ints vs floats. Study the C/C++ language a bit more
                before you attempt to do tricky numerical conversion things.

                btw, the bit pattern for an int value 4 doesn't yield a float value of 4 ...

                kind regards,

                Jos

                I understand that an int is stored as 16bits and a float is stored as 32bits - and a float pointer pointed to an int will probably grab the 16 bits of the int it is pointed at then the next 16 bits - which does not translate to a 32 bit float. I am asking of a way to solve this problem

                btw - if you don't have a solution you should probably NOT reply instead of replying with a statement like "Study the C/C++ language a bit more
                before you attempt to do tricky numerical conversion things"

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by sidhomsam
                  I understand that an int is stored as 16bits and a float is stored as 32bits - and a float pointer pointed to an int will probably grab the 16 bits of the int it is pointed at then the next 16 bits - which does not translate to a 32 bit float. I am asking of a way to solve this problem

                  btw - if you don't have a solution you should probably NOT reply instead of replying with a statement like "Study the C/C++ language a bit more
                  before you attempt to do tricky numerical conversion things"
                  You're most likely wrong about the 16 bits for an int or you're running a very old
                  system. Better dereference the float value from the float pointer first and cast it
                  to an int after. If you have an int pointer do the same but cast the int value to a
                  float after.

                  Better study the C/C++ language quite a bit more before you attempt to do
                  tricky numerical conversion stuff like the above.

                  Jos

                  Comment

                  • sidhomsam
                    New Member
                    • Sep 2007
                    • 5

                    #10
                    Originally posted by JosAH
                    You're most likely wrong about the 16 bits for an int or you're running a very old
                    system. Better dereference the float value from the float pointer first and cast it
                    to an int after. If you have an int pointer do the same but cast the int value to a
                    float after.

                    Better study the C/C++ language quite a bit more before you attempt to do
                    tricky numerical conversion stuff like the above.

                    Jos

                    The program runs on a an embedded system (zilog Z8F6421) - and ints are 16bits, and floats are 32bits

                    btw embedded systems y require little more skill than programming on an operating system

                    Comment

                    • RRick
                      Recognized Expert Contributor
                      • Feb 2007
                      • 463

                      #11
                      Since your ints are much smaller than a float, you can store the int as a float and not worry about loosing any precision of the int.

                      When you want an int, just set the array value to an int and the compiler will take care of the conversion. You'll probably get a warning about loosing precision, but it should compile.
                      [code=cpp]int val = floatArray[99];
                      int val2 = (int) floatArray[102]; // Should stop compiler warnings[/code]
                      Your previous attempt to cast between ints and floats is not going to work. You can't cast between pointer types and expect a valid answer. Some numeric conversion needs to take place.

                      Comment

                      • sidhomsam
                        New Member
                        • Sep 2007
                        • 5

                        #12
                        Originally posted by kreagan
                        I found this article while trying out your problem.

                        Void Pointers

                        Hopefully this is exactly what you needed. Plus it shows you how to use function pointers! Yummy!
                        KREAGAN!!!

                        That was it !! Void Pointers with casting did the trick!!!!

                        Thanks alot

                        here is what I ended up with:

                        void sub_example(voi d);


                        int int_variable;
                        void *float_pointer[2];
                        float sam=3.14159;
                        float sum_sam;


                        main()
                        {

                        int_variable = 4;

                        float_pointer[0] = &int_variabl e;
                        float_pointer[1] = &sam;

                        sub_example();

                        sum_sam = *(int*)float_po inter[0] + *(float*)float_ pointer[1];
                        printf("%f",sum _sam );

                        }

                        void sub_example(voi d)
                        {
                        int_variable = 12;

                        }





                        My final output was "15.14159"

                        Thanks again

                        Comment

                        • kreagan
                          New Member
                          • Aug 2007
                          • 153

                          #13
                          Originally posted by sidhomsam
                          KREAGAN!!!

                          That was it !! Void Pointers with casting did the trick!!!!

                          Thanks alot
                          Yay! Very nice. Thanks for letting us know.

                          ps. be weary of incrementing void pointers

                          Comment

                          Working...