If I have a char *null_terminated_array="hello"
, I think printf("%zu",strlen(null_terminated_array))
should print 5
(not including '\0'
) and not 6
(including '\0'
).
Asked
Active
Viewed 65 times
-3

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

user786
- 3,902
- 4
- 40
- 72
-
3This assumption is correct. `strlen` does not count the terminating `0` byte. Did you get different results? Then you need to show a more complete piece of code. – Gerhardh Oct 01 '21 at 14:11
-
See the C++ Q&A [`sizeof` string literal](https://stackoverflow.com/q/1392200/15168) — valid for C too. See also the C standard [§7.1.1 Definition of terms](http://port70.net/~nsz/c/c11/n1570.html#7.1.1) for the C library: "A string is a contiguous sequence of characters terminated by and including the first null character. … The length of a string is the number of bytes preceding the null character …". See also [§6.5.3.4](http://port70.net/~nsz/c/c11/n1570.html#6.5.3.4), [§6.4.5](http://port70.net/~nsz/c/c11/n1570.html#6.4.5) and [§7.24.6.3](http://port70.net/~nsz/c/c11/n1570.html#7.24.6.3). – Jonathan Leffler Oct 01 '21 at 14:17
-
@user786 What is the problem to read the description of the function?! – Vlad from Moscow Oct 01 '21 at 14:19
-
@Gerhardh no different result. its working as the assumption – user786 Oct 01 '21 at 14:26
-
@VladfromMoscow I have a function like this `void tm(char ***path)` can u please tell how to change second character of string that I am getting in `tm(char ***path)` function from some other function? – user786 Oct 01 '21 at 14:29
-
@user786 You can ask the question here at SO. – Vlad from Moscow Oct 01 '21 at 14:31
-
@user786 If you have a parameter `char ***path` you are almost certainly doing something wrong. (There is a term "[three star programmer](http://wiki.c2.com/?ThreeStarProgrammer)" and it is not a compliment! :-) ) – Steve Summit Oct 01 '21 at 14:31
-
@SteveSummit I didn't do it but I think I should know how to do it – user786 Oct 01 '21 at 14:36
-
@VladfromMoscow I have asked a question can u please look https://stackoverflow.com/questions/69407760/threee-indirection-pointer-how-to-change-indirected-strings-index-character – user786 Oct 01 '21 at 14:40
-
1What you think is correct. What is your actual question? – Jabberwocky Oct 01 '21 at 15:10
-
@user786 and please don't post another unrelated question in a comment, but post an all new question. You have >2000 of reputation, you should know that. – Jabberwocky Oct 01 '21 at 15:12
1 Answers
1
Yes that is what you would expect.
Just be cautious if you were tempted to use sizeof
rather than strlen
on the array:
char *null_terminated_array="hello";
char char_array[6]="hello";
printf("null_terminated_array = %s\n",null_terminated_array);
printf("strlen(null_terminated_array) = %zu\n",strlen(null_terminated_array));
printf("sizeof(null_terminated_array) = %zu\n",sizeof(null_terminated_array));
printf("\n");
printf("char_array = %s\n",char_array);
printf("strlen(char_array) = %zu\n",strlen(char_array));
printf("sizeof(char_array) = %zu\n",sizeof(char_array));
for the platform that I ran this on this gives
null_terminated_array = hello
strlen(null_terminated_array) = 5
sizeof(null_terminated_array) = 8
char_array = hello
strlen(char_array) = 5
sizeof(char_array) = 6
The apparent discrepancy for sizeof(null_terminated_array)
is to do with automatic memory allocation using blocks of 32bits (platform dependent).

ChrisBD
- 9,104
- 3
- 22
- 35
-
can u please tell `sizeof()` does it check for null termination or not if argument in char pointer or it does not check for null termination. I am thinking if it does not check for null termination in string argument then how can sizeof an object can be found? Is there a thing that I am missing completely can u tell more – user786 Oct 01 '21 at 15:18
-
Sizeof does not "check" anything. The compiler tries to work out the amount of memory that the variable or type takes. If it's a `char[10]` it will be replaced by `10`. If it's something like a `char[]` or `char *` that points to a string literal, it will be replaced by the length of the string literal AND the trailing `\0`. This will only be true if the string literal is in the same scope (function or global). As e.g. a function argument it would only get the size of a pointer, because the compiler can't assume from which scope and with which arguments the function is called. – Cheatah Oct 01 '21 at 22:13