Thread: Pointer to Pointer + Pointer casting; Need Explanation Please.

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    9

    Pointer to Pointer + Pointer casting; Need Explanation Please.

    Hi all,

    Have a little question about pointer casting (the meaning of it);

    Here is my code (working on Eclipse):
    Code:
    #include<stdio.h>
    
    int main(void){
    
    int x = 5;
    int *myPtr1;
    int **myPtr2;
    
    myPtr1 = &x;
    
    printf("The addr of x variable is: %p\n", &x);
    printf("The addr myPtr1 refers to, is: %p\n", myPtr1);
    printf("The value myPtr1 refer to: %d\n", *myPtr1);
    printf("The addr of myPtr1 variable is: %p\n\n", &myPtr1);
    
    *myPtr2 =(int *) myPtr1;
    
    printf("The 1-st addr myPtr2 refers to: %p\n", myPtr2);
    printf("The 2-nd addr myPtr2 refers to:%p\n", *myPtr2);
    printf("The value myPtr2 refers to: %d\n", **myPtr2);
    printf("The addr of myPtr2 variable is: %p\n", &myPtr2);
    
    return 0;
    }

    The output example is:

    Code:
    The addr of x variable is: 0xbfd96624
    The addr myPtr1 refers to, is: 0xbfd96624
    The value myPtr1 refer to: 5
    The addr of myPtr1 variable is: 0xbfd96628
    
    The 1-st addr myPtr2 refers to: 0xb7759000
    The 2-nd addr myPtr2 refers to:0xbfd96624
    The value myPtr2 refers to: 5
    The addr of myPtr2 variable is: 0xbfd9662c

    Still, I can't truly understand what does this line mean:

    Code:
    *myPtr2 =(int *) myPtr1;
    By this, I don't sure if the text descriptions I've provided in"printf" functions are correct.


    The "roots" of this question come from my investigation of "strod" function source code. There I've found something similar and just tried to understand what they have done there.

    Furthermore:

    In case this line is commented:
    Code:
    printf("The addr of myPtr2 variable is: %p\n", &myPtr2);
    Then Eclipse underlines this line:
    Code:
    *myPtr2 =(int *) myPtr1;
    and saying:
    Code:
    myPtr2 is used uninitialized in this function
    But in case the above "printf" is used, all works good.


    Any help/explanation/clarification will be highly appreciated.

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,737
    Quote Originally Posted by bravoelf
    Still, I can't truly understand what does this line mean:
    Code:
    *myPtr2 =(int *) myPtr1;
    It means: cast myPtr1 to be a pointer to int, then assign the result to what myPtr2 points to.

    Actually, myPtr1 is already a pointer to int, so this would suffice:
    Code:
    *myPtr2 = myPtr1;
    That said, it looks like you did not set myPtr2 to point to something valid, hence *myPtr2 actually results in undefined behaviour. This corresponds exactly to the warning message that you observed: "myPtr2 is used uninitialized in this function".
    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
    Aug 2015
    Posts
    9
    Quote Originally Posted by laserlight View Post
    It means: cast myPtr1 to be a pointer to int, then assign the result to what myPtr2 points to.

    Actually, myPtr1 is already a pointer to int, so this would suffice:
    Code:
    *myPtr2 = myPtr1;
    My apologies, but this is still not fully clear to me. What happens to myPtr2 when I use casting (reading Deitel's book all the day though ^_^) is it kinda "downgraded" to "int *" insted "int **"?


    I've checked your code change and this is the result I get:
    Code:
    The addr of x variable is: 0xbfad3da4
    The addr myPtr1 refers to, is: 0xbfad3da4
    The value myPtr1 refer to: 5
    The addr of myPtr1 variable is: 0xbfad3da8
    
    The 1-st addr myPtr2 refers to: 0xb76e9000
    The 2-nd addr myPtr2 refers to:0xbfad3da4
    The value myPtr2 refers to: 5
    The addr of myPtr2 variable is: 0xbfad3dac
    And still I get a warning message when the last "printf" is commented.


    The other situation is when:
    Code:
    myPtr2 = &myPtr1;
    In this case I get fully consistent address picture:
    Code:
    The addr of x variable is: 0xbfee18e4
    The addr myPtr1 refers to, is: 0xbfee18e4
    The value myPtr1 refer to: 5
    The addr of myPtr1 variable is: 0xbfee18e8
    
    The 1-st addr myPtr2 refers to: 0xbfee18e8
    The 2-nd addr myPtr2 refers to:0xbfee18e4
    The value myPtr2 refers to: 5
    The addr of myPtr2 variable is: 0xbfee18ec

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,737
    Quote Originally Posted by bravoelf
    My apologies, but this is still not fully clear to me. What happens to myPtr2 when I use casting (reading Deitel's book all the day though ^_^) is it kinda "downgraded" to "int *" insted "int **"?
    The cast is for myPtr1, not myPtr2. The "downgrade" happens because of the asterisk in front of myPtr2 that dereferences it, i.e., if myPtr2 points to something, *myPtr2 is that something. So, if myPtr2 points to a pointer to an int, then *myPtr2 is that pointer to an int.

    And still I get a warning message when the last "printf" is commented.
    That is because removing the cast changes nothing: the cast was unnecessary to begin with.

    What you need to fix is the fact that myPtr2 does not point to anything, yet you dereferenced it. Here's a simpler example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int *p;
        printf("The value p refers to: %d\n", *p);
        return 0;
    }
    Here's the above example, fixed:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n = 123;
        int *p = &n;
        printf("The value p refers to: %d\n", *p);
        return 0;
    }
    It is fixed because p points to n prior to dereferencing it. Notice the similiarity with:
    Code:
    myPtr2 = &myPtr1;
    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

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    9
    Thanks for your explanation laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting a pointer to char to a pointer to int
    By kkk in forum C Programming
    Replies: 5
    Last Post: 05-18-2011, 11:54 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  4. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread