[SOLVED] QSring and Char
-
when i run the program with the below code, i get an error: invalid conversion from 'const char*' to 'char'
@char tt = "test";@
yet when i change it too const char to fix the above error, i get the same error that i can't solve.
@const char tt = "test";@
I am trying to store "test" as a variable with char and its not working. I can store it with QString.
-
I moved this to the C++ Gurus forum, as it is no Qt specific problem (and nothing "guru" too, but anyways...)
You ran into a simple C syntax problem: char is a single character, so it's ok to write
@
const char tt = 'a';
@but "test" is a string, so you need to assign it to a char array:
@
const char *tt = "test";
@spot the asterisk before the tt - it makes tt a char array (= C string) as opposed to a single character without it.
-
thank you Volker.
but why would i want to use char instead of Qstring?I basically got the answer to this above question when I ran the new code. warning: deprecated conversion from string constant to 'char*'. I guess char is deprecated and I should use QString instead.
-
Neither char nor char* are deprecated. This are valid C constructs and will never go away. Your error is most probably the fact that you use "char *" instead of "const char *". Again, this is basic C/C++ stuff.
But you are right, in that you should avoid C strings (char *) whenever possible, because they are cumbersome to work with and you can easily run into trouble with the memory management.
So, your code would look better this way:
@
// this calls the QString constructor with a const char * argument:
QString tt2("abc");
@If you have non-ASCII strings, consider one of the fromXXX static methods of [[Doc:QString]].