指针指向整数,字符,及字符串时,相应地址取法
- 指针指向整数时:
1 #include <iostream> 2 3 int main(void) 4 { 5 using namespace std; 6 int a = 10; 7 int *p = &a; 8 9 cout << "sizeof(p) = " << sizeof(p) << endl; 10 cout << "sizeof(&p) = " << sizeof(&p) << endl; 11 cout << "sizeof(*p) = " << sizeof(*p) << endl; 12 13 cout << "p = " << p << endl; 14 cout << "&a = " << &a << endl; 15 cout << "*p = " << *p << endl; 16 cout << "&p = " << &p << endl; 17 18 return 0; 19 } 20 /******************************************** 21 * sizeof(p) = 8 22 * sizeof(&p) = 8 23 * sizeof(*p) = 4 24 * p = 0x7ffe4aff08ac 25 * &a = 0x7ffe4aff08ac 26 * *p = 10 27 * &p = 0x7ffe4aff08a0 28 * *****************************************/
p是指向整数10的指针,从代码看出,sizeof(p)和sizeof(&p)的值都是8,那是因为p和&p都是表示地址(我的系统存储地址的大小是8个字节,你的可能不一样),它们的类型都是int*, 但要注意的是,p这个地址和&p是不同的,要说明它们的不同点,实质上要牵扯到指针存值的性质。在我的实验中,p = 0x7ffe4aff08ac ,恰恰,&a 也是等于 0x7ffe4aff08ac ,说明指针p中存储的是a的地址。&p是什么?是存储p的地址。这个很简单,之所以说这个,是因为接下来我要探索点别的。
- 指针指向字符时:
1 #include <iostream> 2 3 using namespace std; 4 5 int main(void) 6 { 7 char a = 'a'; 8 char *p = &a; 9 cout << sizeof(*p) << " = sizeof(*p)" << endl; 10 11 cout << sizeof(p) << " = sizeof(p)" << endl; 12 cout << "p address: " << (void*)p << endl; 13 cout << "a address: " << (void*)&a << endl; 14 cout << sizeof(&p) << " = sizeof(&p)" << endl; 15 cout << "&p address: " << &p << endl; 16 17 return 0; 18 } 19 20 /*************************************** 21 * 1 = sizeof(*p) 22 * 8 = sizeof(p) 23 * p address: 0x7ffc4b5d942f 24 * a address: 0x7ffc4b5d942f 25 * 8 = sizeof(&p) 26 * &p address: 0x7ffc4b5d9420 27 * *************************************/
- 指针指向字符串时的地址和数组上的字符串:
1 #include <iostream> 2 3 int main(void) 4 { 5 using namespace std; 6 7 cout << endl << "******* char* *******" << endl; 8 char *str = "Busui"; 9 cout << "*str = " << *str << endl; 10 cout << "str[0] = " << str[0] << endl; 11 cout << "&str[0] = " << &str[0] << endl; 12 cout << "str = " << str << endl; 13 14 cout << "str[0] address: " << (void*)&str[0] << endl; 15 cout << "str address: " << (void*)str << endl; 16 cout << "&str address: " << &str << endl; 17 18 cout << "sizeof(str): " << sizeof(str) << endl; 19 cout << "sizeof(&str): " << sizeof(&str) << endl; 20 21 22 cout << endl <<"*******array********"<< endl; 23 char str1[] = "Busui"; 24 cout << "*str1 = " << *str1 << endl; 25 cout << "str1[0] = " << str1[0] << endl; 26 cout << "&str1[0] = " << &str1[0] << endl; 27 cout << "str1 = " << str1 << endl; 28 29 cout << "str1[0] address: " << (void*)&str1[0] << endl; 30 cout << "str1 address: " << (void*)str1 << endl; 31 cout << "&str1 address: " << &str1 << endl; 32 33 cout << "sizeof(str1): " << sizeof(str1) << endl; 34 35 cout << "sizeof(str1): " << sizeof(str1) << endl; 36 cout << "sizeof(&str1): " << sizeof(&str1) << endl; 37 38 return 0; 39 40 } 41 42 /********************************** 43 * ******* char* ******* 44 * *str = B 45 * str[0] = B 46 * &str[0] = Busui 47 * str = Busui 48 * str[0] address: 0x400e17 49 * str address: 0x400e17 50 * &str address: 0x7ffdfcf1a9a8 51 * sizeof(str): 8 52 * sizeof(&str): 8 53 * 54 * *******array******** 55 * *str1 = B 56 * str1[0] = B 57 * &str1[0] = Busui 58 * str1 = Busui 59 * str1[0] address: 0x7ffdfcf1a9a0 60 * str1 address: 0x7ffdfcf1a9a0 61 * &str1 address: 0x7ffdfcf1a9a0 62 * sizeof(str1): 6 63 * sizeof(&str1): 8 64 * *********************************/
我们来看数组:str1[0] address,str1 address,&str1 address 的地址均为:0x7ffdfcf1a9a0,说明编译器除了在栈为存储指针而分配了空间外,还为指针中(即数组中)的内容分配了内存空间。值得注意的是,str[0](str) 的地址虽然与&str的相同,但是它们是不一样的东西。str[0](str)是数组第一个元素‘B‘的地址,而&str是整个数组的地址。另外一个要点是:sizeof(str): 8,但是sizeof(str1): 6 。为什么呢?其实这是因为str1的类型为char [6],str的类型则是char*,这也是指针和数组在存储字符串是的不同点。那些总把指针和数组混为一谈的同学注意咯。