Thread: Pointer Type Casting

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Pointer Type Casting

    I am confused with pointer type casting. I read that i cannot type case an integer pointer to float pointer. Am I correct? The code is
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int value=0x20FE;
        float *fp;
        int *p = &value;
        fp = (float *)(&value);
        printf("int value = %f\n", fp);
        printf("char ptr value = %d\n",p);
        return 0;
    }
    or can i take blindly that pointers should never be type casted?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Quote Originally Posted by Satya
    I read that i cannot type case an integer pointer to float pointer. Am I correct?
    The relevant rule is:
    Quote Originally Posted by C11 Clause 6.3.2.3 Paragraph 7
    A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.
    So, if the alignment works out, you can cast a pointer to int to pointer to float. But there would be nothing else useful you could do with it other than converting it back again since the representation of an int likely makes no sense when reinterpreted as a float, and that's what you would be doing with the cast: reinterpreting the int that the pointer points to as if it were a float.

    Quote Originally Posted by Satya
    or can i take blindly that pointers should never be type casted?
    No, as you can see from the rule, you can safely cast your pointer to int to be a pointer to char (or unsigned char, or signed char). This makes sense as you're reinterpreting the int as a sequence of bytes... but that is exactly what the underlying storage of the int consists of! Additionally:
    Quote Originally Posted by C11 Clause 6.3.2.3 Paragraphs 1, 4
    A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

    Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Thank you very much for the reply. Can you please explain me
    "If the resulting pointer is not correctly aligned for the referenced type,"
    with one example? How do i know whether it is aligned or not aligned correctly? Please help.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Consider:
    Code:
    float value = 1.23f;
    double *p = (double*)(&value);
    Assuming that float is 4 bytes and double is 8 bytes, p now points to an object that isn't actually big enough for p to point to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-23-2016, 09:46 AM
  2. Help with pointer type casting
    By Eclipse07 in forum C Programming
    Replies: 14
    Last Post: 12-03-2013, 03:48 PM
  3. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  4. Pointer type casting
    By vlrk in forum C Programming
    Replies: 2
    Last Post: 08-11-2008, 07:33 AM
  5. pointer type casting
    By RoshanX in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2006, 12:35 PM

Tags for this Thread