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.