0

Good evening,

a new evening a new question :D

May you know what i've not understood here, in the declaration is a 2nd const, i've no idea what is the difference between them. When i run the code it sounds both nice.

void theStrlnMethod() {
//TODO why need i to add const two times here? :D
const char* const TEXT1 = "123";
std::cout << TEXT1 << "\nenthält " << strlen(TEXT1) << " Zeichen\n";

//TODO does that also work without the 2nd const?
const char* TEXT_JUST_ONE_CONST = "123";
std::cout << TEXT_JUST_ONE_CONST << "\nenthält " << strlen(TEXT_JUST_ONE_CONST) << " Zeichen\n";
}

output:

123
enthõlt 3 Zeichen
123
enthõlt 3 Zeichen

1 Answers1

1

A constant pointer to a constant character array: you cant' change neither the pointer, nor the stuff pointed to.

In the second case you could (but are not compelled to) modify TEXT_JUST_ONE_CONST i.e. make TEXT_JUST_ONE_CONST point to other memory.

(Sorry, I didn't notice it's a duplicate)

GGa
  • 97
  • 4