What is the difference between char * const and const char *?

前端 未结 19 1133
执笔经年
执笔经年 2020-11-22 03:31

What\'s the difference between:

char * const 

and

const char *
19条回答
  •  不思量自难忘°
    2020-11-22 03:42

    Here is a detailed explanation with code

    /*const char * p;
    char * const p; 
    const char * const p;*/ // these are the three conditions,
    
    // const char *p;const char * const p; pointer value cannot be changed
    
    // char * const p; pointer address cannot be changed
    
    // const char * const p; both cannot be changed.
    
    #include
    
    /*int main()
    {
        const char * p; // value cannot be changed
        char z;
        //*p = 'c'; // this will not work
        p = &z;
        printf(" %c\n",*p);
        return 0;
    }*/
    
    /*int main()
    {
        char * const p; // address cannot be changed
        char z;
        *p = 'c'; 
        //p = &z;   // this will not work
        printf(" %c\n",*p);
        return 0;
    }*/
    
    
    
    /*int main()
    {
        const char * const p; // both address and value cannot be changed
        char z;
        *p = 'c'; // this will not work
        p = &z; // this will not work
        printf(" %c\n",*p);
        return 0;
    }*/
    

提交回复
热议问题