C-strings are null-terminated which means that in a char-array, the char at index strlen() is a byte with all bits set to 0. I’ve seen code where, instead of '\0', the integer 0 is used. But since sizeof(int) > sizeof(char), this might actually write beyond the allocated space for the array – am I wrong? Or does the compiler implicitely cast a an int to char in such a situation?
C-strings are null-terminated which means that in a char-array, the char at index strlen()
Share
Yes you are wrong – the zero value will be truncated. After all, if you write:
you would not expect the compiler to write beyond the bounds of the variable c, I hope. If you write something like this:
then the compiler should warn you of the truncation. GCC produces:
but still nothing will be written beyond the bounds of the variable.