其他分享
首页 > 其他分享> > 059.指针-const修饰指针

059.指针-const修饰指针

作者:互联网

#include <iostream>
using namespace std;
int main()
{
    //1.const修饰指针-常量指针
    int a = 10;
    int b = 10;
    const int* p = &a;
    //指针指向的值不可以改,指针指向可以改
    //*p=20;错误
    p = &b;//正确


    //2.const修饰常量-指针常量
    //指针的指向不可以改,指针指向的值可以改
    int* const p2 = &a;
    *p2 = 100;//正确
    //p2 = &b;//错误


    //3.const修饰指针和常量
    const int* const p3 = &a;
    //指针的指向和指针指向的值都不可以改
    //*p3 = 100;//错误
    //p3 = &b;//错误

    system("pause");
    return 0;
}

 

标签:const,常量,指向,int,059,修饰,指针
来源: https://www.cnblogs.com/ceovs/p/15226722.html