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?
An actual C-type string is one bigger than the number of its characters, since it needs a terminating null character.
Therefore,
char y[4] = {'t','e','s','t'};
doesn't form a string, since it's four characters.char y[] = "test";
orchar y[5] = "test";
would form a string, since they'd have a character array of five characters ending with the null-byte terminator.strlen
works with strings. String is defined as a sequence (array) of characters terminated with\0
character.Your
x
points to a string. So,strlen
works fine withx
as an argument.Your
y
is not a string. For this reason, passingy
tostrlen
results in undefined behavior. The result is meaningless and unpredictable.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):The following is not a null terminated array of characters:
Part of
strlen()
's contract is that it be provided with a pointer to a null terminated string. Since that doesn't happen withstrlen(y)
, you get undefined behavior. In your particular case, you get6
returned, but anything could happen, including a program crash.From C99's 7.1.1 "Definition of terms":
You need to null-terminate y.
strlen() basically takes the pointer you give it and counts the number of bytes until the next NULL in memory. It just so happened that there was a NULL two bytes later in your memory.
This
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. Withy
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:
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:Modern implementations will likely not fetch individual bytes or even use CPU intrinsics, but this is the basic algorithm.