0

For the following piece of code:

char a[] = "Apple";
char *s[] = {"Apple"};
printf("%d %d\n", sizeof(a), sizeof(s[0]));

The output is:

6 4

Can someone tell me why sizeof() is giving different outputs?

EDIT: I did intend to type sizeof() originally, but typed strlen() instead. I apologize for the same. I have edited the statement now.

Kevin Richards
  • 570
  • 1
  • 6
  • 23
  • 2
    Err… maybe because you use `strlen` for `s[0]` (which doesn't count the 0-terminator)… (`sizeof s[0]`, however, would give `sizeof(char *)`) – mafso Jul 17 '14 at 13:55
  • Because the second test is not sizeof, but strlen? strlen does not include the terminating null character. Even if it was sizeof, however, it would return the size of the pointer to the string. – perh Jul 17 '14 at 13:56
  • @mafso Oops! My bad. I have edited the question statement. I did intend to write sizeof() only. – Kevin Richards Jul 17 '14 at 15:53
  • See e.g. [here](http://stackoverflow.com/questions/13672162/sizeof-arrays-and-pointers) and maybe [here](http://stackoverflow.com/questions/17424078/char-pointer-as-function-parameter). And `char *s[] = {"Apple"};` is (besides the additional identifier) equivalent to `char *tmp = "Apple"; char *s[] = { tmp };` not (what you seem to assume) to `char s[][6] = { "Apple" };` (cf. second part of my last comment). Related: http://c-faq.com/aryptr/index.html. And last not least: Use `%zu` to print `size_t` variables, `%d` is for `int` (and `sizeof` yields an object of type `size_t`). – mafso Jul 17 '14 at 16:06
  • 1
    -1 for changing the question!!!! – David Heffernan Jul 17 '14 at 21:04

8 Answers8

5

sizeof gives you the number of chars allocated to a while strlen gives you the length of useable string in a.

unxnut
  • 8,509
  • 3
  • 27
  • 41
5

sizeof(a) is the size of the array, which contains the terminator as well as the 5 printable characters.

strlen(s[0]) gives the length of the string, excluding the terminator, since that is what strlen is specified to do.

UPDATE: sizeof(s[0]) is the size of a pointer. There's no way to determine the size of an array given just a pointer to it.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
3

the \0 counts as part of the size in memory of the string, but the length of the string itself, given by strlen(), is only given by the characters before the \0 is encountered.

Ranic
  • 486
  • 4
  • 12
3

A character array declared like this:

char a[] = "Apple";

has, according to the language specification, a null-terminator. Therefore, the length of the array is 6. There are 5 characters, and then the null terminator.

On the other hand, strlen() returns the number of characters that precede the null terminator. Which is 5.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

The sizeof operator yields the size (in bytes) of its operand.

In this statement

char a[] = "Apple";

array a is initialized by characters of string literal "Apple" that includes the terminating zero. In fact this record is equivalent to

char a[] = { 'A', 'p', 'p', 'l', 'e', '\0'; };

So the size in bytes of a is equal to 6.

Standard C function strlen counts symbols in a string until it encounters the terminating zero. So

strlen( a )

will return 5 that is the number of characters in the array that are before the terminating zero.

Take into account that you could write for example

char a[100] = "Apple";

In this case sizeof( a ) will yield 100 because you explicitly specified the number of bytes that the array will occupy. However it was initialized only with 6 characters of the string literal. So how to find how many actual data are in the character array? For this purpose function strlen was introduced that to distinguish the size of a character array and the number of actual data in the character array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Because in C there is no string type. String is a character array which is NULL terminated. strlen() counts the characters until the NULL character, whereas sizeof() actually returns the amount of memory used up by the charater array.

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
1

a is an array of chars, which contains 6 elements, hence sizeof returns 6 (the length of the string including zero termination).

s is an array of pointers to char. The pointer size is 4 bytes. sizeof(s[0]) returns the size of the first element, which is pointer, i.e. its size is 4.

Austin Moore
  • 1,414
  • 6
  • 20
  • 43
user1028643
  • 11
  • 1
  • 1
1

When you define:

char a[] = "Apple";

It means an array of characters, which equals to the following definition:

char a[] = {'A', 'p', 'p', 'l', 'e', '\0'}; // '\0' is the string termination character which equals to 0

Since char type size is 1, the sizeof(a) returns 6 which is the size of the whole array.

Nevertheless, when you define:

char *s[] = {"Apple"};

It means an array of char pointer. Hence sizeof(s[0]) return the size of its first element which equals to sizeof(char*). For a 32-bit platform, sizeof(char*) = 4. If you do it on an 64-bit platform, 8 is the expected value.

Long_GM
  • 181
  • 1
  • 10