deprecated conversion from string constant to '

2020-07-05 07:11发布

I am working with strings .

Whenever I execute the following program I get an error as deprecated conversion from string constant to 'char' in c* on the line char *p = "hello"

What am i doing wrong?

What does this error mean ?How can i correct it?

My code is:

#include<stdio.h>
int main()
{
    char *p = "hello";
    printf("%s",p+1);
    return 0;
}

标签: c
2条回答
【Aperson】
2楼-- · 2020-07-05 07:38

This should be a warning (though you may have set your compiler to treat warnings as errors, which is often a good idea).

What you want is: char const *p = "hello"; instead.

Attempting to modify a string literal gives undefined behavior. The const prevents you from doing that by accident (i.e., code that attempts to write via the pointer won't compile, unless you remove the const qualifier, such as with a cast).

查看更多
女痞
3楼-- · 2020-07-05 07:45

This is a warning, because "Hello" string is a constant and you are trying to store that in non const char*. To slove the problem either make it

const char* p = "Hello" or char[] p = "Hello"

查看更多
登录 后发表回答