What is the difference between char s[] and char *s?
In C, one can use a string literal in a declaration like this:
char s[] = "hello";
or like this:
char *s = "hello";
So what is the difference? I want to know what actually happens in terms of storage duration, both at compile and run time.
The difference here is that
char *s = "Hello world";
will place "Hello world"
in the read-only parts of the memory, and making s
a pointer to that makes any writing operation on this memory illegal.
While doing:
char s[] = "Hello world";
puts the literal string in read-only memory and copies the string to newly allocated memory on the stack. Thus making
s[0] = 'J';
legal.
First off, in function arguments, they are exactly equivalent:
void foo(char *x);
void foo(char x[]); // exactly the same in all respects
In other contexts, char *
allocates a pointer, while char []
allocates an array. Where does the string go in the former case, you ask? The compiler secretly allocates a static anonymous array to hold the string literal. So:
char *x = "Foo";
// is approximately equivalent to:
static const char __secret_anonymous_array[] = "Foo";
char *x = (char *) __secret_anonymous_array;
Note that you must not ever attempt to modify the contents of this anonymous array via this pointer; the effects are undefined (often meaning a crash):
x[1] = 'O'; // BAD. DON'T DO THIS.
Using the array syntax directly allocates it into new memory. Thus modification is safe:
char x[] = "Foo";
x[1] = 'O'; // No problem.
However the array only lives as long as its contaning scope, so if you do this in a function, don't return or leak a pointer to this array - make a copy instead with strdup()
or similar. If the array is allocated in global scope, of course, no problem.
This declaration:
char s[] = "hello";
Creates one object - a char
array of size 6, called s
, initialised with the values 'h', 'e', 'l', 'l', 'o', '