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

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

What\'s the difference between:

char * const 

and

const char *
相关标签:
19条回答
  • 2020-11-22 03:44
    // Some more complex constant variable/pointer declaration.
    // Observing cases when we get error and warning would help
    // understanding it better.
    
    int main(void)
    {
      char ca1[10]= "aaaa"; // char array 1
      char ca2[10]= "bbbb"; // char array 2
    
      char *pca1= ca1;
      char *pca2= ca2;
    
      char const *ccs= pca1;
      char * const csc= pca2;
      ccs[1]='m';  // Bad - error: assignment of read-only location ‘*(ccs + 1u)’
      ccs= csc;    // Good
    
      csc[1]='n';  // Good
      csc= ccs;    // Bad - error: assignment of read-only variable ‘csc’
    
      char const **ccss= &ccs;     // Good
      char const **ccss1= &csc;    // Bad - warning: initialization from incompatible pointer type
    
      char * const *cscs= &csc;    // Good
      char * const *cscs1= &ccs;   // Bad - warning: initialization from incompatible pointer type
    
      char ** const cssc=   &pca1; // Good
      char ** const cssc1=  &ccs;  // Bad - warning: initialization from incompatible pointer type
      char ** const cssc2=  &csc;  // Bad - warning: initialization discards ‘const’
                                   //                qualifier from pointer target type
    
      *ccss[1]= 'x'; // Bad - error: assignment of read-only location ‘**(ccss + 8u)’
      *ccss= ccs;    // Good
      *ccss= csc;    // Good
      ccss= ccss1;   // Good
      ccss= cscs;    // Bad - warning: assignment from incompatible pointer type
    
      *cscs[1]= 'y'; // Good
      *cscs= ccs;    // Bad - error: assignment of read-only location ‘*cscs’
      *cscs= csc;    // Bad - error: assignment of read-only location ‘*cscs’
      cscs= cscs1;   // Good
      cscs= cssc;    // Good
    
      *cssc[1]= 'z'; // Good
      *cssc= ccs;    // Bad - warning: assignment discards ‘const’
                     //                qualifier from pointer target type
      *cssc= csc;    // Good
      *cssc= pca2;   // Good
      cssc= ccss;    // Bad - error: assignment of read-only variable ‘cssc’
      cssc= cscs;    // Bad - error: assignment of read-only variable ‘cssc’
      cssc= cssc1;   // Bad - error: assignment of read-only variable ‘cssc’
    }
    
    0 讨论(0)
  • 2020-11-22 03:46

    The const modifier is applied to the term immediately to its left. The only exception to this is when there is nothing to its left, then it applies to what is immediately on its right.

    These are all equivalent ways of saying "constant pointer to a constant char":

    • const char * const
    • const char const *
    • char const * const
    • char const const *
    0 讨论(0)
  • 2020-11-22 03:47

    I presume you mean const char * and char * const .

    The first, const char *, is a pointer to a constant character. The pointer itself is mutable.

    The second, char * const is a constant pointer to a character. The pointer cannot change, the character it points to can.

    And then there is const char * const where the pointer and character cannot change.

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

    I remember from Czech book about C: read the declaration that you start with the variable and go left. So for

    char * const a;
    

    you can read as: "a is variable of type constant pointer to char",

    char const * a;
    

    you can read as: "a is a pointer to constant variable of type char. I hope this helps.

    Bonus:

    const char * const a;
    

    You will read as a is constant pointer to constant variable of type char.

    0 讨论(0)
  • 2020-11-22 03:50
    1. Constant pointer: A constant pointer can point only to a single variable of the respective data type during the entire program.we can change the value of the variable pointed by the pointer. Initialization should be done during the time of declaration itself.

    Syntax:

    datatype *const var;
    

    char *const comes under this case.

    /*program to illustrate the behaviour of constant pointer */
    
    #include<stdio.h>
    int main(){
      int a=10;
      int *const ptr=&a;
      *ptr=100;/* we can change the value of object but we cannot point it to another variable.suppose another variable int b=20; and ptr=&b; gives you error*/
      printf("%d",*ptr);
      return 0;
    }
    
    1. Pointer to a const value: In this a pointer can point any number of variables of the respective type but we cannot change the value of the object pointed by the pointer at that specific time.

    Syntax:

    const datatype *varor datatype const *var

    const char* comes under this case.

    /* program to illustrate the behavior of pointer to a constant*/
    
       #include<stdio.h>
       int main(){
           int a=10,b=20;
           int const *ptr=&a;
           printf("%d\n",*ptr);
           /*  *ptr=100 is not possible i.e we cannot change the value of the object pointed by the pointer*/
           ptr=&b;
           printf("%d",*ptr);
           /*we can point it to another object*/
           return 0;
        }
    
    0 讨论(0)
  • 2020-11-22 03:52

    Lots of answer provide specific techniques, rule of thumbs etc to understand this particular instance of variable declaration. But there is a generic technique of understand any declaration:

    Clockwise/Spiral Rule

    A)

    const char *a;
    

    As per the clockwise/spiral rule a is pointer to character that is constant. Which means character is constant but the pointer can change. i.e. a = "other string"; is fine but a[2] = 'c'; will fail to compile

    B)

    char * const a;
    

    As per the rule, a is const pointer to a character. i.e. You can do a[2] = 'c'; but you cannot do a = "other string";

    0 讨论(0)
提交回复
热议问题