指针指向整数,字符,及字符串时,相应地址取法

  • 指针指向整数时:
     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   * *************************************/

     

    输入字符时其实和输入整数差不多。唯一的不同点就是:当指针指向char型时,若想用cout通过p输出指针p的值(这个值就是p存储的地址)时,就需要用(void*),无独有偶,如果像输出&a,也必须这样(void*)&a。如果不加上(void*)会发生什么呢?会将字符a打印出来,并接着将内存中随后各个字节解释为要打印的字符,直到遇到空字符为止。
  • 指针指向字符串时的地址和数组上的字符串:
     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   * *********************************/
    View Code

     

    这里,首先我们可以发现,str[0] 和str(它们都是:0x400e17)的地址格式与&str(0x7ffdfcf1a9a8)不一样,事实上,str[0](或str) 表示的都是字符串“Busui”的首地址,我们在初始化str时是这样的:char *str = "Busui" ,这个初始化定义的是一个普通指针str,并没有定义空间来存放"Busui",所以编译器得帮我们找地方来放"Busui",显然,通过str的地址格式:0x400e17得知这里的"Busui"被当成常量并把它放到程序的常量区了,这是由编译器决定的。而开始初始化定义的普通指针的格式为char*,且编译器会在栈中为它分配了内存,我们通过取址符&的到它的地址为:0x7ffdfcf1a9a8 ,这说明了地址的位置确实在栈中。
    我们来看数组: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*,这也是指针和数组在存储字符串是的不同点。那些总把指针和数组混为一谈的同学注意咯。

转载于:https://www.cnblogs.com/busui/p/5747612.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值