Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deprecated conversion from string constant to 'char*' in c [duplicate]

Tags:

c

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;
}
like image 663
user2227862 Avatar asked May 27 '13 05:05

user2227862


2 Answers

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).

like image 182
Jerry Coffin Avatar answered Oct 04 '22 14:10

Jerry Coffin


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"

like image 27
Daemon Avatar answered Oct 04 '22 14:10

Daemon