C指针,&,*,指针的指针

C指针: 指向变量的地址,想象成房间号

      &: 取地址符号

      *:间接访问符号, 访问p所存地址的内容

#include <iostream>

int main(int argc, const char * argv[]) {

    int m = 5;
    int *p = &m;
    
    std::cout << "p = " << p << std::endl; //0x7ffeefbff5bc
    std::cout << "*p = " << *p << std::endl; //5
    
    return 0;
}

 

#include <iostream>

int main(int argc, const char * argv[]) {

    int m = 5;
    int *p = &m;
    
    std::cout << "p = " << p << std::endl; //0x7ffeefbff5bc
    std::cout << "*p = " << *p << std::endl; //5
    
    std::cout << "&p = " << &p << std::endl;//0x7ffeefbff5a0
    
    int **pp = &p; //& 不可以使用两次
    
    int b = **pp;
    std::cout << "b = " << b << std::endl; //5
    std::cout << "*pp = " << *pp << std::endl;//0x7ffeefbff58c
    std::cout << "pp = " << pp << std::endl;//0x7ffeefbff570
    return 0;
}

 

另外注意指针要初始化,否则成为野指针。

参考 https://www.cnblogs.com/mymindview/p/5247485.html

posted @ 2019-03-02 11:41  liuw_flexi  阅读(1851)  评论(0编辑  收藏  举报