As long as I'm working with strings, I've got one more (silly?) question:
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[8] = "assiduus";
char t[] = "assiduus";
char u[] = { 'a', 's', 's', 'i', 'd', 'u', 'u', 's' };
printf("%d, %d\n", strlen(s), strcmp(s, t));
printf("%d, %d\n", strlen(u), strcmp(u, t));
return 0;
}
The session:
Code:
assiduus@ubuntu:~$ cc tmp.c
assiduus@ubuntu:~$ ./a.out
8, 0
16, 97
As you can see, there's no place to add the null character as far as "s" is concerned and, therefore, the compiler doesn't add one. From what I understand, it means that "s" cannot be treated as a string and dealt with through string-related functions. And yet, when "measured" and compared with "t" everything works just fine. How does strlen know where "s" ends? And when I try to recreate the same situation by hand with "u", it fails. What's the secret?