I wanted to check out what strlen did if the string it was acting upon overflowed...
gave this output:Code:#include <stdio.h> #include <string.h> int main() { char str[10]; sprintf( str, "four" ); printf( "\n%s - %d", str, strlen( str )); strcat( str, "four" ); printf( "\n%s - %d", str, strlen( str )); strcat( str, "four" ); printf( "\n%s - %d", str, strlen( str )); printf( "\n" ); return 0; }
four - 4
fourfour - 8
fourfourfour - 12
Is this behaviour predictable i.e. can I check that a string has not overflowed by using strlen to ensure that the length of the string is less than the space originally allocated to it?
Thanks.



LinkBack URL
About LinkBacks



