059.指针-const修饰指针

时间:2021-09-06 05:48:01   收藏:0   阅读:15
#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;
}

 

原文:https://www.cnblogs.com/ceovs/p/15226722.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!