Possible problems with compiling code when the GCC version is high in Linux
The problem is this, first look at the function prototype:
void SomeFunc (char *somestr);
And look at this function call:
SomeFunc ("I ' m a string!");
Combine these two things and compile them with the latest g++ to get a warning in the title.
Why, then? the original char * behind the meaning is: Give me a string, I want to modify it.
In theory, the literal constants we pass to a function cannot be modified .
Therefore, the comparison and rationale of the method is to modify the parameter type to const char *.
The meaning behind this type is: give me a string and I'll just read it.
It's a natural extension.
What if I have to pass literal constants and string variables? ...... Overload
Experiment:
Detailed explanation of such warnings for deprecated conversion from string constant to ' char * '
Suppose you want to use a variable of type char*, sometimes pointing to a string, sometimes pointing to another string. The starting code is like this:
Char *msg;
msg = "Hello";
msg = "Good-bye";
The compiler gives a two-paragraph warning of the code, saying that "deprecated conversion from string constant to ' char *" means you have no ability to modify the contents of the string. If the code is written like this:
char *msg = "Hello";
*msg = ' J ';
printf ("%s/n", "Hello");
The compiler, by compiling, actually converts MSG-oriented content from "Hello" to "jello", and the correct solution is tomsg declared as a pointer to the invariant string:
const char *msg;
msg = "Hello";
msg = "Good-bye";
This code can compile successfully and change the value that msg points to, but ifyou assign the pointer to the point:
*msg = ' J ';
An error will be generated and a string constant cannot be modified
Note the following code, which does not have a warning when it compiles and no errors occur:
const char *msg;
Char buf[10]; Note that Char *buf cannot be used;
sprintf (buf, "%03d/n", 7);
msg = BUF;
Changing the content of the BUF is possible because it is not declared as a constant. In this case, MSG will point to a string, "007/n". Like this statement
*buf = ' x ';
Execution will be compiled correctly, but like
*msg = ' x ';
a warning will be generated because MSG points to content that is not allowed to change
There is also a way tousing casts, using a cast means you know what will happen and you don't need the compiler to make a decision for you, for example, the following code will not produce a warning:
Char *msg;
msg = (char *) "Hello";
But once you use the cast, the compiler does not have an error or warning when compiling the following statement
*msg = ' J ';
This error will always exist, but will not be discovered until run time. It would be rather troublesome to find the wrong points at that time, much more troublesome than the compiler reminds you. Therefore, it is best not to use casts on strings.
Constant pointer
Depending on the location of the constant, there are four possible scenarios:
const char* Const MSG_0;
const char *msg_1;
char* Const msg_2;
Char *msg_3;
Where msg_0 is a constant pointer pointing to a const string. This declaration compiler gives a warning because the msg_0 point is not initialized, and subsequent statements cannot be assigned to the MG_0, as
const char Const *MSG_0 = "Hello";
Will compile successfully, but
*msg_0 = ' J ';
Msg_0 = "Good-bye"; will produce an error
Msg_1 can point to either a const string or a mutable string, but cannot modify the contents of the string it points to.
Compiling msg_2 This statement will present the same error as compiling msg_0. because the pointer is a constant, it should be assigned first . If you have already assigned a value at the beginning, it can modify the contents of the string that you are pointing to, such as:
Char buf[10];
char * Const MSG_2 = BUF;
In this code, Msg_2 point to buf[0], and always point to this address, will not change;
For Msg_3, there is not much to say. You can change the pointer, or you can change what the pointer points to.