S
Seebs
Seebs said:Not really. a is an array of 14 characters. In *some contexts*, a referenceSo in the following expressing
a = "Hello, world!";
"a" is a pointer type (char*) and "Hello World" has the type char[14].
to an array is converted into a pointer to its first element. But that
doesn't make that pointer into an object which has storage. And
the string literal "Hello, world!" actually is a pointer; there is an
array-like thing somewhere, but we don't see it as an object.
That's incorrect. The string literal -- well, the string literal itself
is a token in a C source file. But it's associated with an array object
with static storage duration that exists during program execution.
That's a real object. It doesn't have a name, but neither does an
object allocated via malloc(). You can apply sizeof to get its size, or
unary "&" to get its address (which is of type char(*)[LEN+1]).
A string literal is an expression of array type; it's no more "actually
a pointer" than any other array expression is. Like any such
expression, it's implicitly converted to a pointer in most, but not all,
contexts.
You're right. I hadn't thought about this in detail in ages, and it took
me a while to come up with an example, but that sizeof() yields the number
of characters in the string, not the size of a pointer, is compelling.
Fascinating. I wonder when I got that one screwed up; probably at least 15
years ago. It turns out that the type of string literals rarely affects my
world, I guess.
-s