size of character array and size of character pointer
Asked Answered
S

6

20

I have a piece of C code and I don't understand how the sizeof(...) function works:

#include <stdio.h>

int main(){
   const char  firstname[] = "bobby";
   const char* lastname = "eraserhead";
   printf("%lu\n", sizeof(firstname) + sizeof(lastname));
   return 0;
}

In the above code sizeof(firstname) is 6 and sizeof(lastname) is 8.

But bobby is 5 characters wide and eraserhead is 11 wide. I expect 16.

Why is sizeof behaving differently for the character array and pointer to character?

Can any one clarify?

Shotgun answered 23/6, 2013 at 11:21 Comment(1)
Both sizeof and strlen() give a result of type size_t. Use %zu, not %lu to print size_t values. Or, if your compiler doesn't support %zu, convert to a known type and use the appropriate format for that type.Alas
G
26

firstname is a char array carrying a trailing 0-terminator. lastname is a pointer. On a 64bit system pointers are 8 byte wide.

Gurgitation answered 23/6, 2013 at 11:23 Comment(3)
Well it is clear that OP was trying to find out the size of the string i.e. string length and add both of them as he says " why not 5+10=15?",Is it really possible to get string length via sizeof or strlen() is more suitable? what do you say?Harem
@PHIfounder: As a "string" in C is represented by a sequence of characters terminated by a '\0' character, use strlen() to get a "string"'s length. This works on "real" character arrays, as also on pointers pointing to the latter. In this context it is of no primary importance how and where the characters are stored. In fact the variable holding the string does not necessarily need to match the "string"'s length. If on the other hand one it interested in the amount of bytes a variable is declared to use, use the sizeof operator.Gurgitation
And the number of bytes needed to store a string s (or pointed to by s) is strlen(s) + 1.Alas
F
6

sizeof an array is the size of the total array, in the case of "bobby", it's 5 characters and one trailing \0 which equals 6.

sizeof a pointer is the size of the pointer, which is normally 4 bytes in 32-bit machine and 8 bytes in 64-bit machine.

Facia answered 23/6, 2013 at 11:25 Comment(0)
B
4

The size of your first array is the size of bobby\0. \0 is the terminator character, so it is 6.

The second size is the size of a pointer, which is 8 byte in your 64bit system. Its size doesn't depends on the assigned string's length.

Below answered 23/6, 2013 at 11:24 Comment(0)
W
4

how the sizeof(...) function works

sizeof() looks like a function but it's not a function. A function computes something at run-time.

sizeof() asks the compiler, at compile-time, how much memory it allocates for the argument. BTW sizeof() has no idea how much of it you actually use later at run time. In other words, you've "hardcoded" the printf arguments in your example.

Why is sizeof behaving differently for the character array and pointer to character?

A pointer rarely requires the same amount of memory as an array.

In general, the amount of memory allocated for a pointer is different to what is allocated for its pointee.

Wotan answered 9/2, 2020 at 1:14 Comment(0)
R
3

firstname is an array of 6 chars, including the terminating '\0' character at the end of the string. That's why sizeof firstname is 6.

lastname is a pointer to char, and will have whatever size such a pointer has on your system. Typical values are 4 and 8. The size of lastname will be the same no matter what it is pointing to (or even if it is pointing to nothing at all).

Rector answered 23/6, 2013 at 11:25 Comment(0)
P
2

firstname[] is null-terminated, which adds 1 to the length.

sizeof(lastname) is giving the size of the pointer instead of the actual value.

Perryperryman answered 23/6, 2013 at 11:25 Comment(1)
*lastname is a single char, so sizeof *lastname will always be 1.Rector

© 2022 - 2024 — McMap. All rights reserved.