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

相关文章:

  • 2021-08-08
  • 2022-12-23
  • 2021-07-02
  • 2022-01-01
  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
猜你喜欢
  • 2021-07-12
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案