X
Xiaozhong
I recently find int *p is a GENUINE pointer, which means, p itself is
an integer that takes 4 bytes in memory; however int a[3], where the
array name a is a VIRTUAL pointer, whose physical address is just
itself and could not be retrieved.
I find that int *p creates a *genuine* pointer to an integer, where p
is per se an 4-byte integer stored in memory. The value of p indicates
the address of the integer it points to. Therefore, *p, p, &p should
all be different. So far so good.
However, as is said in many textbook, int a[4] create an array, with
the array name 'a' as a pointer to this array.
Therefore I just speculated that *a,a, &a should also be different
values; I further guessed that creating and initializing a[4] consumes
4*4bytes to hold the value of a[4] and another 4 byte pointer a to
addressing the starting point of the array. But it is not the case in
Visual C++ 2005. &a=a in the result. a itself does not have an
address.
Who can explain this? All comments are welcomed.
#include <iostream>
using namespace std;
void main(){
int a[]={1,2,3,4};
for (int i=0;i<4;i++){
printf("a[%d]:%4d|addr:%6x\n",i,a,&(a));
}
printf("\n\n");
printf("&a: \t%8x\n",&a);
printf("a: \t%8x\n",a);
printf("*a:\t%8x\n",*a);
int *p;
p=new int(0xff);
printf("&p:\t%8x\n",&p);
printf("p:\t%8x\n",p);
printf("*p:\t%8x\n",*p);
}
a[0]: 1|addr:12ff64
a[1]: 2|addr:12ff68
a[2]: 3|addr:12ff6c
a[3]: 4|addr:12ff70
&a: 12ff64
a: 12ff64
*a: 1
&p: 12ff60
p: 475fd0
*p: ff
an integer that takes 4 bytes in memory; however int a[3], where the
array name a is a VIRTUAL pointer, whose physical address is just
itself and could not be retrieved.
I find that int *p creates a *genuine* pointer to an integer, where p
is per se an 4-byte integer stored in memory. The value of p indicates
the address of the integer it points to. Therefore, *p, p, &p should
all be different. So far so good.
However, as is said in many textbook, int a[4] create an array, with
the array name 'a' as a pointer to this array.
Therefore I just speculated that *a,a, &a should also be different
values; I further guessed that creating and initializing a[4] consumes
4*4bytes to hold the value of a[4] and another 4 byte pointer a to
addressing the starting point of the array. But it is not the case in
Visual C++ 2005. &a=a in the result. a itself does not have an
address.
Who can explain this? All comments are welcomed.
#include <iostream>
using namespace std;
void main(){
int a[]={1,2,3,4};
for (int i=0;i<4;i++){
printf("a[%d]:%4d|addr:%6x\n",i,a,&(a));
}
printf("\n\n");
printf("&a: \t%8x\n",&a);
printf("a: \t%8x\n",a);
printf("*a:\t%8x\n",*a);
int *p;
p=new int(0xff);
printf("&p:\t%8x\n",&p);
printf("p:\t%8x\n",p);
printf("*p:\t%8x\n",*p);
}
a[0]: 1|addr:12ff64
a[1]: 2|addr:12ff68
a[2]: 3|addr:12ff6c
a[3]: 4|addr:12ff70
&a: 12ff64
a: 12ff64
*a: 1
&p: 12ff60
p: 475fd0
*p: ff