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

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

What\'s the difference between:

char * const 

and

const char *
相关标签:
19条回答
  • 2020-11-22 03:40

    const * char is invalid C code and is meaningless. Perhaps you meant to ask the difference between a const char * and a char const *, or possibly the difference between a const char * and a char * const?

    See also:

    • What are const pointers (as opposed to pointers to const objects)?
    • Const in C
    • Difference between const declarations in C++
    • C++ const question
    • Why can I change the values of a const char* variable?
    0 讨论(0)
  • 2020-11-22 03:40

    char * const and const char *?

    1. Pointing to a constant value

    const char * p; // value cannot be changed

    1. Constant pointer to a value

    char * const p; // address cannot be changed

    1. Constant pointer to a constant value

    const char * const p; // both cannot be changed.

    0 讨论(0)
  • 2020-11-22 03:41

    I would like to point out that using int const * (or const int *) isn't about a pointer pointing to a const int variable, but that this variable is const for this specific pointer.

    For example:

    int var = 10;
    int const * _p = &var;
    

    The code above compiles perfectly fine. _p points to a const variable, although var itself isn't constant.

    0 讨论(0)
  • 2020-11-22 03:42
    1. const char* x Here X is basically a character pointer which is pointing to a constant value

    2. char* const x is refer to character pointer which is constant, but the location it is pointing can be change.

    3. const char* const x is combination to 1 and 2, means it is a constant character pointer which is pointing to constant value.

    4. const *char x will cause a compiler error. it can not be declared.

    5. char const * x is equal to point 1.

    the rule of thumb is if const is with var name then the pointer will be constant but the pointing location can be changed , else pointer will point to a constant location and pointer can point to another location but the pointing location content can not be change.

    0 讨论(0)
  • 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<stdio.h>
    
    /*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;
    }*/
    
    0 讨论(0)
  • 2020-11-22 03:44

    Another thumb rule is to check where const is:

    1. before * => value stored is constant
    2. after * => pointer itself is constant
    0 讨论(0)
提交回复
热议问题