Monday, March 9, 2009

What's the difference between these three declarations?

char *a = "abc";
char b[] = "abc";
char c[3] = "abc";

Answer:The first declares a pointer-to-char, initialized to point to a four-character array somewhere in (possibly read-only) memory containing the four characters a b c \0. The second declares an array (a writable array) of 4 characters, initially containing the characters a b c \0. The third declares an array of 3 characters, initially containing a b c. (The third array is therefore not an immediately valid string.)

No comments: