0

I am really confused what is exactly const in const char is because i can change the value. Is the Pointer const or what is this? and what is the difference to const char or char* or char const???

Denmuell
  • 87
  • 1
  • 10
  • 4
    Does this answer your question? [What is the difference between const int\*, const int \* const, and int const \*?](https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const) – Ardent Coder May 22 '20 at 15:38
  • It is an array of `const char`, the pointer itself is not `const`. – Cory Kramer May 22 '20 at 15:38
  • https://stackoverflow.com/questions/2812875/how-const-keyword-works-in-c , https://stackoverflow.com/questions/4949254/const-char-const-versus-const-char/4949283 and https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const – Jose May 22 '20 at 15:41

3 Answers3

2
char * p = ...

is a pointer to a char. You can change the pointer, as well as the value the pointer is pointing to.

char const * p = ...
const char * p = ...

are equivalent, and are pointers to a char const. The pointer can be pointed anywhere, but it must always point to a char const.

char const * const p = ...
const char * const p = ...

are equivalent, and declare a const pointer to a char const. You can't change what the pointer is pointing to, or the value at what the pointer is pointing to.

char * const p = ...

is a const pointer to a char. You can change the value that is pointed to, but you can't change the pointer to point somewhere else.

cigien
  • 57,834
  • 11
  • 73
  • 112
1

I am really confused what is exactly const in const char

The char is const. I.e. the pointed type is const char.

because i can change the value.

Indeed, the pointer is not const, therefore it can be made to point elsewhere by changing its value.

Is the Pointer const

No. It is a non-const pointer to const.

and what is the difference to const char

It is a non-mutable character.

or char*

It is a pointer to a character.

or char const???

Same as const char.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

No, you can't change the value.

const char* ptr = "thing";
ptr[2] = 'o';  // error

It's a pointer to const chars.

You can, however, make the pointer point to something else, because it's not a const char* const. :)

Const applies to the left, unless there's nothing there, in which case it applies to the right.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35