Warning: deprecated conversion from String constant to "char *" SOLUTION

Source: Internet
Author: User

LinuxEnvironment whenGccWhen the version is relatively high, compileCodePossible problems

The problem is as follows: first look at the function prototype:

Void somefunc (char * somestr );

Let's look at this function call:

Somefunc ("I'm a string! ");

Combine these two items with the latestG ++Compile the command to get the warning in the title.

Why? OriginalThe meaning behind char * is: Give me a string and I want to modify it.

In theory, weThe literal constants passed to the function cannot be modified..

Therefore, the comparison and rationale are as follows:Change the parameter typeConst char *.

The meaning behind this type is:Give me a string. I just need to read it.

It's a natural extension.

What if I want to pass both a literal constant and a string variable?......Heavy Load

Lab:

Deprecated conversion from String constant to 'Char *'

Suppose you want to use a char * type variable, sometimes pointing to a string, sometimes pointing to another string. The initial code is like this:
Char * MSG;
MSG = "hello ";
MSG = "good-bye ";

The compiler will give two warnings to this code section, saying "deprecated conversion from String constant to 'Char * '", that is, you are not able to modify the content of the string. If you write the code like this, for example:
Char * MSG = "hello ";
* MSG = 'J ';
Printf ("% s \ n", "hello ");

Through compilation, the compiler will actually change the content that MSG points to from "hello" to "Jello". The correct solution isMSG declares a pointer to a constant string.:
Const char * MSG;
MSG = "hello ";
MSG = "good-bye ";

This code can be compiled and the value pointed to by MSG can be changed as expected.Assign a value to the pointer:
* MSG = 'J ';
An error is generated, and a String constant cannot be modified.

Note that the following code does not generate warnings or errors during compilation:
Const char * MSG;
Char Buf [10]; // note that char * Buf cannot be used;
Sprintf (BUF, "% 03d \ n", 7 );
MSG = Buf;

It is possible to change the Buf content because it is not declared as a constant. In this case, MSG will point to a string, "007 \ n". Such a statement
* Buf = 'X ';
Will be compiled and executed correctly,
* MSG = 'X ';
A warning is generated because the content pointed to by MSG cannot be changed.

Another method is:Use forced conversion, Using forced conversion means that you know what will happen and do not need the compiler to make a judgment for you. For example, the following code will not generate a warning:
Char * MSG;
MSG = (char *) "Hello ";

However, once you use forced conversion, the compiler will not generate errors or warnings when compiling the following statements:
* MSG = 'J ';

This error will always exist, but will not be detected until it is running. At that time, it would be quite troublesome to find another error point, which is much more troublesome than the compiler reminds you. Therefore, it is best not to use forced conversion for strings.

Constant pointer

Depending on the position of the constant, there are four possible situations:
Const char * const msg_0;
Const char * msg_1;
Char * const msg_2;
Char * msg_3;

Msg_0 is a constant pointer pointing to a const string. This declaration compiler will give a warning, because the point of msg_0 is not initialized, and subsequent statements cannot assign values to mg_0, as shown in
Const char const * msg_0 = "hello ";
Will be compiled successfully,
* Msg_0 = 'J'; or
Msg_0 = "good-bye"; errors may occur.

Msg_1 can point to either a const string or a variable string, but cannot modify the content of the string it points.

Compile the msg_2 statement with the same error as compile msg_0.Because the pointer is a constant, it should be assigned a value first..If the value has been assigned at the beginning, it can modify the content of the string to which it points.Such:
Char Buf [10];
Char * const msg_2 = Buf;
In this Code, msg_2 points to Buf [0] and always points to this address, which will not change;

There is not much to say about msg_3. You can change the pointer or the content pointed to by the pointer;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.