再回顾const int* p, int const* p,int* const p 的区别

慈云数据 2024-05-01 技术支持 91 0

目录

  • 一、const int* p 和 int const* p (常量指针
    • 1.1 p 指向的内存不能通过指针p 修改
    • 1.2 其他指针可以修改p 指向的内存
    • 1.3 指针p 可以重新指向其他地址
    • 二、int* const p (指针常量)
      • 2.1 p 定义的时候初始化
      • 2.2 p 定义的时候未初始化
      • 2.3 可以通过 p 修改指向内存存放的值
      • 三、const int * const p

        一、const int* p 和 int const* p (常量指针)

        const int* p 和 int const* p 两者都是一样,表示的都是p 指向的是内容不能通过指针p 去修改。 注意:这里不表示p 指向的区域不能被修改,如果一个 int * p2 指针也指向同一个区域,可以通过p2 指针修改这个区域的内容。

        在这里插入图片描述

        1.1 p 指向的内存不能通过指针p 修改

        void test_1_func( void )
        {
            int buf_1[5] = {1, 2, 3, 4, 5};
            const int *p = buf_1;
            int *p1 = buf_1;
            p[0] = 8;
        }
        

        编译上述代码,编译器提示:

        main.c: In function ‘test_1_func’:
        main.c:10:10: error: assignment of read-only location ‘*p’
           10 |     p[0] = 8;
              |   
        

        1.2 其他指针可以修改p 指向的内存

        void test_1_func( void )
        {
            int buf_1[5] = {1, 2, 3, 4, 5};
            const int *p = buf_1;
            int *p1 = buf_1;
            int i = 0;
            p1[0] = 8;
            printf( "print buf_1:\n" );
            for ( i = 0; i  
        

        代码执行结果如下:

        第一个元素已经被修改成8。

        print buf_1:
        8,2,3,4,5,
        

        1.3 指针p 可以重新指向其他地址

        void test_2_func( void )
        {
            int buf_1[5] = {1, 2, 3, 4, 5};
            int buf_2[5] = {11, 12, 13, 14, 15};
            int i = 0;
            const int *p = buf_1;
            printf( "p pointer to buf_1:\n" );
            for ( i = 0; i  
        

        代码执行结果如下:

        p pointer to buf_1:
        1,2,3,4,5,
        p pointer to buf_2:
        11,12,13,14,15,
        

        二、int* const p (指针常量)

        int* const p const 修饰的是指针p,表示p 只能指向一个地址,并且是在定义p 的时候指向该地址,中途不能指向其他地址。

        2.1 p 定义的时候初始化

        void test_3_func( void )
        {
            int buf_1[5] = {1, 2, 3, 4, 5};
            int *const p = buf_1 ;
            printf( "p pointer to buf_1:\n" );
            for ( int i = 0; i  
        

        代码编译,运行结果如下:

        p pointer to buf_1:
        1,2,3,4,5,
        

        2.2 p 定义的时候未初始化

        void test_3_func( void )
        {
            int buf_1[5] = {1, 2, 3, 4, 5};
            int *const p = NULL;
            p = buf_1;
        }
        

        编译结果如下:

        main.c: In function ‘test_3_func’:
        main.c:43:7: error: assignment of read-only variable ‘p’
           43 |     p = buf_1;
              |    
        

        2.3 可以通过 p 修改指向内存存放的值

        void test_3_func( void )
        {
            int buf_1[5] = {1, 2, 3, 4, 5};
            int *const p = buf_1;
            p[0] = 6;
            p[1] = 7;
            printf( "p pointer to buf_1:\n" );
            for (int i = 0; i  
        

        程序运行结果如下:

        p pointer to buf_1:
        6,7,3,4,5,
        

        三、const int * const p

        结合前面的介绍,可以知道:

        • p 指向的内容不能通过p 进行修改
        • p 指针也只能指向一个地址,不能修改
          void test_4_func(void)
          {
              int buf_1[5] = {1, 2, 3, 4, 5};
              int buf_2[5] = {11, 12, 13, 14, 15};
              const int * const p = buf_1;
              p[0] = 6; //error
              p = buf_2; //error
          }
          

          代码编译结果如下:

          main.c: In function ‘test_4_func’:
          main.c:58:10: error: assignment of read-only location ‘*p’
             58 |     p[0] = 6;
                |          ^
          main.c:59:7: error: assignment of read-only variable ‘p’
             59 |     p = buf_2;
                |   
          
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon