1) p points to a
2) the address of a is stored where p is pointing
3) p points to memory address 1
4) prints the address stored in p
5) prints the integer pointed to by p
If
int main()
{
int a=1;
int *p;
how do the following statements differ
1) p=&a;
2) *p=&a;
3) p=a;
4) printf("%d",p);
5) printf("%d",*p);
and what does int **a mean
2 answers
Hello Ray :)
(Sorry if i have a bad English)
- int a = 1 : You declared an integer variable, (imagine that is a box that contain the value 1 : [1].
-int *p : You declared a pointer of integer . When we declare a pointer we need to put * before the variable . But * have an other meaning, when we use the pointers :
Imgine that p is an arrow that indicate integer boxes ->[int ?]. p will contain the address of the box, and *p allows us to see what's in the box .
1] Imagine that we give to "p" the address of "a", than the pointer p "->" will move and stuck to the box "a" like this : ->[ 1] "
2]we put into the box that was pointed by "p ->[?int]" the location of a . ( useless :) )
3] p will point to another memory slot, and we give to him an other box [] (in these case it will change nothing :) : p will point the same box "a" )
And like Steve said :
4] prints the address stored in p
5] prints the integer pointed to by p
(Sorry if i have a bad English)
- int a = 1 : You declared an integer variable, (imagine that is a box that contain the value 1 : [1].
-int *p : You declared a pointer of integer . When we declare a pointer we need to put * before the variable . But * have an other meaning, when we use the pointers :
Imgine that p is an arrow that indicate integer boxes ->[int ?]. p will contain the address of the box, and *p allows us to see what's in the box .
1] Imagine that we give to "p" the address of "a", than the pointer p "->" will move and stuck to the box "a" like this : ->[ 1] "
2]we put into the box that was pointed by "p ->[?int]" the location of a . ( useless :) )
3] p will point to another memory slot, and we give to him an other box [] (in these case it will change nothing :) : p will point the same box "a" )
And like Steve said :
4] prints the address stored in p
5] prints the integer pointed to by p