OK, but I want you to know that I came up with some different conclusions, way easier ones. I also want to say here that I don't know everything obviously, but that I am piecing together a solution from different sources. Enjoy my train of thought. This one's a doozy.
***If anyone wants the final answer, just go down to the last code window***
Here is a much less complicated program I came up with, similar to yours, and throwing the same error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
int main(){
const char* string1 = "string1";
const char string2[] = "string2";
string1 = string2; // This one works fine.
string2 = string1; // If you hover over string2 (should be underlined by red), it will tell you
// that the expression must be a modifiable value, but it gives the same
// error when you attempt compilation: C2440
std::cin.get();
return 0;
}
|
I tried different things to see what else I could find out, so I tried to make string2 = some other string instead. string2 was still underlined with the same message, and the compiler throws an error saying "cannot convert from const char [one size] to const char [another size]", as I tried strings of different sizes. So I matched the number of letters, like here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <string>
int main(){
const char* string1 = "string1";
const char string2[] = "string2";
string1 = string2;
string2 = "Vlad567"; // string2 hover-over displays exactly:
// "const char string2[8] = "string2"
// Error: string must be a modifiable lvalue (lvalue not a typo)
string2 = string1; // same hover-over message
std::cin.get();
return 0;
}
|
When compile attempted, this comes out:
1 2
|
error C2106: '=' : left operand must be l-value (at line string2 = "Vlad567";)
error C2440: '=' : cannot convert from 'const char *' to 'const char [8]' (at line string2=string1;)
|
I looked up that first error, which is a different kind, and found this page:
http://msdn.microsoft.com/en-us/library/wxy5f14h.aspx
Which gives this example of what can throw this error, namely something like:
1 = a;
SOOO what I am getting from this is that once a const string array is defined, that is it. It cannot be changed period. Whenever you see it again, you might as well type in the literal string it represents.
On the other hand, when a const char pointer is defined, it CAN be changed. Check out this other forum topic:
http://www.cplusplus.com/forum/beginner/10602/
There, kbw states that const char * is a pointer to a constant string. A const char * can point to a different string, but the string it is currently pointing to cannot change.
I hope this has answered your question and gave some clarification.
Referring to your original code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include<iostream>
using namespace std;
const char z[]="Blank"; // z is now set as a constant char array - it CANNOT be changed.
void callthis(const char* m);
int main(){
callthis("Test");
return 0;
}
void callthis(const char* m){ // m points to the constant char array "Test"
z=m; // error C2440: '=' : cannot convert from 'const char *' to 'const char [6]'
}
/*
#include<iostream>
using namespace std;
void callthis(const char t[]);
const char* x = "Blank"; // x POINTS to constant string "Blank"
int main(){
callthis("Test");
return 0;
}
void callthis(const char t[]){ // const char t[] = "Test".....OK, I lose. See comment at end.
t=x; // OK
}
*/
|
I still couldn't figure it out. So when you hover-over t in the above statement, it says guess what? It IS a const char *. I don't know why. It seems like it should be read as a literal string, but my guess is that when you are passing the string as an array, since you are not passing it by address, t is simply a pointer to the original string you had listed.
In other words, a pass by value is the same thing as pointing to what is being passed, essentially meaning it is a pointer to the array, which MEANS that the line t=x is legal. So NOW, here is your original code again with attached final comments. Enjoy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include<iostream>
using namespace std;
const char z[]="Blank"; // z is set to a constant character array. It cannot change period.
void callthis(const char* m);
int main(){
callthis("Test");
return 0;
}
void callthis(const char* m){ // m is a pointer to a the const character array "Test"
z=m; // error C2440: '=' : cannot convert from 'const char *' to 'const char [6]'
// Here, const char [6] = const char * is not legal.
}
/*
#include<iostream>
using namespace std;
void callthis(const char t[]);
const char* x = "Blank"; // x is a pointer, pointing to a constant char array with values "Blank"
int main(){
callthis("Test"); // literal string passed
return 0;
}
void callthis(const char t[]){ // This is the key. Passed by value which essentially means this is a POINTER to the original data being passed - "Test" - and NOT that actual string.
t=x; // OK // when you hover-over t in this statement, says "const char * t"
// And that's the game! Here, const char * = const char * is perfectly legal.
}
*/
|