4

For a string name[],can we use strlen(name)+1 and sizeof(name) interchangeably in our code without second thought?Aren't they same?I checked about it and found out even the return type for both is same,size_t.And after all sizeof calculates the size as a multiple of bytes and a character occupies one byte, while strlen() calculates the number of characters excluding the \0 due to which we have to add a 1 to equate it to the result of sizeof.

So unless the size of the character is something other than 1 byte,aren't the two interchangeable and same for all practical purposes?Like while allocating dynamic memory for example....

Thokchom
  • 1,602
  • 3
  • 17
  • 32

2 Answers2

4

For a declared array like char name[10], sizeof(name) is the size of the entire array, regardless of what data is stored in it. strlen(name) returns the number of characters in the array before the first 0. Behavior of strlen is undefined if there aren't any zeros in the array.

kamal pal
  • 4,187
  • 5
  • 25
  • 40
Mike Woolf
  • 1,210
  • 7
  • 11
2

Which book are you reading? sizeof is an operator that tells you how many bytes a type (or in your case, the type of an object) occupies. strlen is a function that tells you where the first '\0' character is located, which indicates the end of the string.

char foo[32] = "hello";
printf("sizeof foo: %zu\n", sizeof foo);
printf("strlen(foo): %zu\n", strlen(foo));

sizeof foo is 32 regardless of what you store in foo, but as you can see strlen(foo) is in fact 5, where you would expect the '\0' (NUL-terminator) to go.

In terms of dynamic allocation, you can expect to store the return value of malloc in a pointer.

char *foo = malloc(32);
strcpy(foo, "hello");
printf("sizeof foo: %zu\n", sizeof foo);
printf("sizeof (char *): %zu\n", sizeof (char *));
printf("strlen(foo): %zu\n", strlen(foo));

In this case, sizeof foo is sizeof (char *), because it gives you the number of bytes in a char * (pointer to char). This is very different to the strlen function because the size of a pointer to char is constant during runtime.

sizeof (char) is always 1. That's required by the C standard. If you find an implementation where that isn't the case, then it isn't a C implementation.

Perhaps your book might explain this, which is why I asked... but there are plenty of poor quality books. Please do answer that question!

autistic
  • 1
  • 3
  • 35
  • 80
  • Damn!! It never occurred to me that `sizeof` will always give the whole size and `strlen()` will give only the characters stored,even though it's a fraction of the total size. – Rüppell's Vulture May 14 '13 at 03:51
  • As of `sizeof` being an operator, I knew that :-) !! – Rüppell's Vulture May 14 '13 at 03:52
  • @Rüppell'sVulture No. Not "characters stored". Read the answer again. `strlen` returns the length of the string, which is the offset of the first `'\0'`. If you copy "\0hello" into the array, instead, then `strlen(foo)` will return 0. – autistic May 14 '13 at 03:53
  • 1
    @Abhineet `NULL` is not `NUL`; One is a pointer, the other is a character. Please don't edit my answers until you understand the syntax of the `sizeof` operator correctly. – autistic May 14 '13 at 10:50