Thread: Need clarification of the following code...

  1. #1
    Registered User EAX101010's Avatar
    Join Date
    Aug 2014
    Posts
    6

    Need clarification of the following code...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double x = 100.1, y;
        int *p;
    
        /* The next statement causes p (which is an
        integer pointer) to point to a double. */
        p = (int *)&x;
    
        /* The next statement does not operate as
        expected. */
        y = *p;
    
        printf("%f", y); /* won't output 100.1 */
        return 0;
    }
    p is a pointer to an integer.

    On line 10, why is p cast as a pointer to an integer?
    Is this an error in the sample code?

    Shouldn't it be cast as:
    Code:
    p = (double *)&x;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EAX101010
    On line 10, why is p cast as a pointer to an integer?
    It is not p that is cast to pointer to int but rather the address of x that is cast. p is already a pointer to int. The reason is to suppress an error/warning that would result if not for the cast, in order to provide the negative demonstration.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > On line 10, why is p cast as a pointer to an integer?
    The cast is to make the type of the RHS (originally double*, but made int* with the cast) match the type of the LHS
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some clarification please.
    By ShadowBoss in forum C Programming
    Replies: 3
    Last Post: 11-19-2011, 02:23 PM
  2. Clarification....
    By CommonTater in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 05:39 PM
  3. clarification
    By WDT in forum C# Programming
    Replies: 1
    Last Post: 01-21-2009, 01:09 AM
  4. need clarification on something...
    By EvBladeRunnervE in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-08-2004, 09:45 AM
  5. A little clarification?
    By Dr Nick in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2002, 01:47 PM