Question regarding array assignment

S

Seebs

Seebs said:
So in the following expressing
a = "Hello, world!";
"a" is a pointer type (char*) and "Hello World" has the type char[14].
Not really. a is an array of 14 characters. In *some contexts*, a reference
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
 
K

Keith Thompson

Seebs said:
Seebs said:
So in the following expressing
a = "Hello, world!";
"a" is a pointer type (char*) and "Hello World" has the type char[14].
Not really. a is an array of 14 characters. In *some contexts*, a reference
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.

And &"hello" yields a pointer of type char(*)[6], not char*.

And

char arr[] = "hello";

*copies* the 6 bytes of "hello" into arr; no pointer decay occurs.

There are three cases where array-to-pointer decay does not occur; the
third applies only to string literals. (No, there aren't four cases;
the reference to _Alignof is an error in N1570, corrected in the
released C11 standard.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top