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

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

What\'s the difference between:

char * const 

and

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

    const always modifies the thing that comes before it (to the left of it), EXCEPT when it's the first thing in a type declaration, where it modifies the thing that comes after it (to the right of it).

    So these two are the same:

    int const *i1;
    const int *i2;
    

    they define pointers to a const int. You can change where i1 and i2 points, but you can't change the value they point at.

    This:

    int *const i3 = (int*) 0x12345678;
    

    defines a const pointer to an integer and initializes it to point at memory location 12345678. You can change the int value at address 12345678, but you can't change the address that i3 points to.

提交回复
热议问题