zoukankan      html  css  js  c++  java
  • 指针指向整数,字符,及字符串时,相应地址取法

    • 指针指向整数时:
       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*,这也是指针和数组在存储字符串是的不同点。那些总把指针和数组混为一谈的同学注意咯。
  • 相关阅读:
    linux内核剖析(六)Linux系统调用详解(实现机制分析)
    Linux内核剖析(五)Linux内核的构建过程
    Linux内核剖析(四)为arm内核构建源码树
    Linux内核剖析(三)构建源码树
    Linux内核剖析(二)Linux内核绪论
    kubectl更新镜像和回滚命令
    Linux登录shell和非登录(交互式shell)环境变量配置
    Elasticsearch7.6学习笔记1 Getting start with Elasticsearch
    docker安装Elasticsearch7.6集群并设置密码
    Jenkinsfile里定义对象和函数,获取git提交人, 发送钉钉通知
  • 原文地址:https://www.cnblogs.com/busui/p/5747612.html
Copyright © 2011-2022 走看看