... no, it's not a synonym. One of the ways in which they differ is in
declarators, as you see.
> int main(int argc, char *argv[])
>
> {
> char ok[] = "abcd";
storage, containing the string "abcd\0"'.
> char *broken = "abcd";
This means 'broken is a pointer to a character, pointing to the literal
string, in unmodified program text, "abcd". (Technically, this should be
'const char *': in C++, this is mandatory.)
Another difference: sizeof(ok) is the size of the array, in bytes;
sizeof(broken) is the size of a pointer to char on this platform.
In *parameter lists*, char *foo and char foo[] are synonyms, because
arrays decay to pointers *when passed to functions*. Otherwise, they are
not. (In this case sizeof() both will give the same answer.)
> /* this is ok */
>
> ok[0]= 'Z';
> printf("ok = %s\n", ok);
>
> /* this segfaults */
>
> broken[0] = 'Z';
the number 3 to a function and then modify it to change the value of 3
throughout the universe, uh, I mean your program. :P
> TIL C is short for 'Confusion'.
it looks like: foo *bar is a *pointer*, not a buffer of anything, so
assigning to it will assign to *some other buffer somewhere else*. foo[]
bar is a convenience notation for making an array on the stack, as long
as necessary, and then initializing it as with memcpy() with an
appropriate size. (Not strcpy() -- you can do this:
char containsnulls[] = "foo\0bar"
which will make an eight-byte array containing "foo\0bar\0". strcpy()
would only fill in the first four bytes of that.)
This general distinction is crucial to get the hang of, because very
often you want to declare things *not* on the stack -- that outlive
their containing function -- and then you genrally have to declare them
as a foo * and allocate and free them by hand. So remembering where your
things live (that char foo[] lives on the stack, while char foo * merely
puts the pointer on the stack while the data lives elsewhere) is
essential.