May 1, 2012 at 8:44pm UTC
Is the following statements correct? Why?
char s[] = "Greetings";
s = “Greetings";
May 1, 2012 at 8:47pm UTC
The second one is not correct, because you cannot assign raw arrays to arrays.
May 1, 2012 at 8:51pm UTC
Thanks, but how about in this case.
char* s = "hello";
s = “how are you?”;
It is correct?
May 1, 2012 at 9:03pm UTC
This is "correct" because the pointer value s is assigned the address of the string constant "how are you?", which is allocated somewhere in memory by the compiler.
May 1, 2012 at 9:03pm UTC
No, because you cannot (anymore) initialize pointers to non-const char with string literals.
Use const char * s = "hello" ;
Last edited on May 1, 2012 at 9:04pm UTC
May 1, 2012 at 9:06pm UTC
Cubbi's right. I typed too fast.