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;
}
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 theconst
qualifier, such as with a cast).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"
orchar[] p = "Hello"