const usage with pointers in C

后端 未结 6 1917
夕颜
夕颜 2020-11-28 10:32

I am going over C and have a question regarding const usage with pointers. I understand the following code:

const char *someArray
6条回答
  •  悲&欢浪女
    2020-11-28 11:29

    //pointer to a const
    void f1()
    {
        int i = 100;
        const int* pi = &i;
        //*pi = 200; <- won't compile
        pi++;
    }
    
    //const pointer
    void f2()
    {
        int i = 100;
        int* const pi = &i;
        *pi = 200;
        //pi++; <- won't compile
    }
    
    //const pointer to a const
    void f3()
    {
        int i = 100;
        const int* const pi = &i;
        //*pi = 200; <- won't compile
        //pi++; <- won't compile
    

    }

提交回复
热议问题