Why am I get this warning?

this is the warning I got from gcc4.5
warning: deprecated conversion from string constant to 'char*'

below is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
  //it would be ok if I change to char s[]  
  char *s = "abcde"; 
  int length = 0;
  while(s[length] != '\0')
  {
    cout<<s[length];
    ++length;
  }
  cout<<endl<<"length = "<<length<<endl;
  --length;
  for(int i = length; i >= 0; --i)
    cout<<s[i];
  cout<<endl;


Thanks a lot
Last edited on
char s[] = "abcde";
or
const char *s = "abcde";

EDIT:
char *s = "abcde"; shouldn't compile (although compilers allow it) because operations on s such as
s[0]='z'; may not work or even crash your program. String literals ("abcde") are kept in read-only section of your executable and you shouldn't modify it for this reason.

char s[] = "abcde"; is OK because string string literal is created on stack (AFAIK)
const char *s = "abcde"; is OK because you use const keyword, making s unmodifable.
Last edited on
Thank you very much
Topic archived. No new replies allowed.