Why should a char array end with a null character

why should the char array end with a null character. is there a reason why I should add a null character to each char array?


they seem to be addressed the same way.

+5
source share
4 answers

In C, if you have a pointer to an array, then there is no way to determine the length of this array. As @AProgrammer points out, designers could leave it to that and forced the programmer to track the length of all character arrays. However, this would make text processing in C even more complicated than it is.

Therefore, the developers of the language have established an agreement that would allow to deduce the length of the string due to the presence of a null character indicating the end of the string.

, strcpy:

char *strcpy(char *destination, const char *source);

C , destination source. , , source.

, , . - , .

+3

A char ( , , memcpy, memmove, strncpy - -, printf ).

NUL (NTCS) NUL. , C , C ( ++, std::string)

+8

, , , , , char . char , , , , C, , .

char - int, float double, char, , .

0

. C/++ char , , .

If you only intend to use arrays as an array of characters, never referring to them as strings, then there is no problem. This is exactly the same as an ints array.

0
source

All Articles