> char string[] = "Hello world";
I thought that gives out warning these days. Isn't the proper type of a string literal the following?
> const char string[] = "Hello world";
Therefore, you can't really modify individual characters there.
> char string[] = "Hello world";
I thought that gives out warning these days. Isn't the proper type of a string literal the following?
> const char string[] = "Hello world";
Therefore, you can't really modify individual characters there.
No, it's a const char pointer. Your declaration actually makes a copy of the "Hello world" string into a new char array, distinct from the string literal itself:
[~]$ cat test.c
#include <stdio.h>
int main(int argc, char ** argv)
{
char a[] = "test2";
printf("sizeof(a): %lu\n", sizeof(a));
return 0;
}
[~]$ gcc -std=c99 -pedantic -Wall -o test test.c
[~]$ ./test
sizeof(a): 6
[~]$http://c-faq.com/aryptr/aryptrequiv.html http://c-faq.com/decl/strlitinit.html
C++ string literals are " const char * " (one of the minor incompatibilities between the languages).
They ought to be "const char[]" but aren't. And, the behavior of "char[]" should be the same as "struct { char[] }", but it isn't.