I've come to bother you all with another probably really simple C question.
Using the following code:
int get_len(char *string){
printf("len: %lu\n", strlen(string));
return 0;
}
int main(){
char *x = "test";
char y[4] = {'t','e','s','t'};
get_len(x); // len: 4
get_len(y); // len: 6
return 0;
}
2 questions. Why are they different and why is y 6? Thanks guys.
EDIT: Sorry, I know what would fix it, I kind of just wanted to understand what was going on. So does strlen just keep forwarding the point till it happens to find a \0? Also when I did strlen in the main function instead of in the get_len function both were 4. Was that just a coincidence?
strlen() — Determine String Length The strlen() function determines the length of string excluding the ending null character. The strlen() function returns the length of string . This example determines the length of the string that is passed to main() .
strlen() function in c The strlen() function calculates the length of a given string. The strlen() function is defined in string. h header file. It doesn't count null character '\0'.
The strlen() function in C returns an integer with the length of the string excluding the NULL character. The strlen() function counts the alphabets, whitespaces, special symbols, and numbers until it encounters the NULL character in the string.
strlen() Syntax The syntax of the strlen() function is: strlen(const char* str); Here, str is the string whose length we need to find out, which is casted to a const char* .
y
is not null-terminated. strlen()
counts characters until it hits a null character. Yours happened to find one after 6, but it could be any number. Try this:
char y[] = {'t','e','s','t', '\0'};
Here's what an implementation of strlen()
might look like (off the top of my head -- don't have my K&R book handy, but I believe there's an implementation given there):
size_t strlen(const char* s)
{
size_t result = 0;
while (*s++) ++result;
return result;
}
This
char y[4] = {'t','e','s','t'};
is not a proper zero-terminated string. It's an array of four characters, without the terminating '\0'
. strlen()
simply counts the characters until it hits a zero. With y
it simply counts over the end of the array until it accidentally finds a zero byte.
Doing this you are invoking undefined behavior. The code might just as well format your hard drive.
You can avoid this by using the special syntax for character array initialization:
char y[] = "test";
This initializes y
with five characters, since it automatically appends a '\0'
.
Note that I also left the array's size unspecified. The compiler figures this out itself, and it automatically re-figures if I change the string's length.
BTW, here's a simple strlen()
implementation:
size_t strlen(const char* p)
{
size_t result = 0;
while(*p++) ++result;
return result;
}
Modern implementations will likely not fetch individual bytes or even use CPU intrinsics, but this is the basic algorithm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With