I get the size of str character array with the following test code:
int main()
{
unsigned char str[] = "abcde";
for(int j = 0; j <= 6; j++) {
printf("str[%u]=%c; %i\n", j, *(str+j), *(str+j));
}
printf("sizeof(str)=%lu\nstrlen(str)=%lu\n", sizeof(str), strlen(str));
return 0;
}
The result is 6, as expected, as can be seen in the screen output here below:
str[0]=a; 97
str[1]=b; 98
str[2]=c; 99
str[3]=d; 100
str[4]=e; 101
str[5]=; 0
str[6]=; 0
sizeof(str)=6 //here it is!
strlen(str)=5
However, if I explicitly include the string dimension (5) in its declaration, like this:
unsigned char str[5] = "abcde";
now the result of sizeof is 5 rather than the expected 6, as can be seen from the function output:
str[0]=a; 97
str[1]=b; 98
str[2]=c; 99
str[3]=d; 100
str[4]=e; 101
str[5]=; 0
str[6]; 8
sizeof(str)=5 // why 5 and not 6???
strlen(str)=5
My question: what is the reason for that different result? Note the termination character is correctly placed after last string character, as can be seen from the examples above. Thanks for the attention.
The string literal
"abcde"used as an initializer of the arraystrhas6characters including the terminating zero character'\0'.But you explicitly declared the array only with
5characters:So it is even unimportant how you are initializing the array because you explicitly specfied its size equal to
5andsizeof( unsigned char )is always equal to1. Sosizeof( str )is evidently equal to5.Pay attention to that in this case your array does not contain a string because it is unable to accommodate the terminating zero character
'\0'of the string literal. So for eample calling the functionstrlenfor the array invokes undefined behavior.Opposite to
CinC++such a declaration is invalid. InC++you should write at leastor as you wrote the first declaration of the array like
In the last case the number of elements in the array is equal to the number of characters in the string literal.
Also to output values of the type
size_tyou should use conversion specifierzuinstead oflubecause in general it is not necessay that the typesize_tis an alias of the typeunsigned long. In some systems it can be an alias of the typeunsigned long long.Frpm the C Standard (7.19 Common definitions <stddef.h>)
So you need to write