char *a = "hello";and
char b[] = "hello";Several differences:
"a" makes a pointer to an anonymous char array. "b" creates an array of chars.
+-+-+-+-+-+
a -> |h|e|l|l|o|
+-+-+-+-+-+
b
+-+-+-+-+-+
|h|e|l|l|o|
+-+-+-+-+-+
sizeof(a) = 4 (on a 32-bit system)
sizeof(b) = 6 (5 chars and '\0')
The pointer "a" is changeable, i.e. it can be set to point to something else. Whereas, "b" cannot change.
The array that pointer "a" points to cannot change. Chars of "b" can change.
a[3] = 'H'; // illegal
b[3] = 'H'; // OK
No comments:
Post a Comment