Reputation: 4787
In the following code:
char *title = "VP";
printf("Sizeof title: %zd | Sizeof *title: %zd | Strlen: %zd\n", sizeof title, sizeof *title, strlen(title));
Sizeof title: 8 | Sizeof *title: 1 | Strlen: 2
It seems like the sizeof title
operates on the pointer to the string (8 bytes for me), and the strlen(title)
predictably gives me 2.
Why does the sizeof *title
produce 1 when dereferencing the pointer rather than 3 (the byte-length of the string)? For example, why does it do that instead of what it would produce for:
printf("%zd\n", sizeof("VP"));
// 3
Upvotes: 3
Views: 1058
Reputation: 123596
The type of title
is char *
, so the type of *title
is char
, not char [3]
. Thus , sizeof *title
is equivalent to sizeof (char)
, which is 1.
title
isn’t the string, it just points to the first element of the string.
Upvotes: 1
Reputation: 409482
The size of a pointer is always the size of the pointer itself, not what it points to. That's because sizeof
is mostly a compile-time operator (the result is evaluated by the compiler) and the compiler can't know what a pointer might point to at run-time.
As for sizeof *title
it's the same as sizeof title[0]
which is a single char
. And the size of a char
is 1
(it's specified to always be 1
by the way, no matter the actual bit-width).
Lastly about sizeof "VP"
. In C all literal strings are really arrays of characters, including the terminating null character. So the literal string "VP"
is an array of three characters, hence its size is 3
.
To make the answer a little bit more complete, I say that the sizeof
operator is mostly compile-time. That of course can't be true for variable-length arrays, where the compiler must insert code to store the actual size of the array in a way that it can be fetched at run-time. If the array decays to a pointer, then all you have is the pointer and again sizeof
returns the size of the pointer itself.
And a note about string literal arrays. While they are technically non-constant arrays, they still can't be modified. Attempting to modify a string literal leads to undefined behavior. Literal strings are thus, in effect, read-only.
Upvotes: 3