const char* mess[] = { "aba" };
Well, the usual normal array ... Note - an array of pointers .
const char* test;
Pointer somewhere - no one knows where, but to an immutable (constant) character.
int n = strlen(mess); for (int i = 0; i < n; i++) {
Well, here it is clear - for all the characters of the first element in the mess array.
if (mess[i] == "a") test[i] = "b"; if (mess[i] == "b") test[i] = "a";
Those. trying to change immutable characters? Compiler against Now, if test were declared as char * test; ... However, it would still not work - it is still impossible to assign a pointer to a string for a character.
And this is very good - because if he resolved this, your next question would be “why does the program crash”? By the way, you already understand why? There are two reasons: going beyond the mess array - there is only one element in it, and the length of the first element is 3. And the second is recording at a random address via test .
And yet - I think that you are here - mess[i] == "a" - they wanted to check something completely different. Now you check if the address stored in the i -th element of the array is equal to the address of the string "a" . Of course not...
It seems to me that you wanted something like this:
const char mess[] = "aba"; char* test = new char[strlen(mess)+1]; strcpy(test,mess); int n = strlen(mess); for (int i = 0; i < n; i++) { if (mess[i] == 'a') test[i] = 'b'; if (mess[i] == 'b') test[i] = 'a'; }