C-string definition in C / C++
What really means by word "C-string" in C / C++? Pointer to char? Array of characters? Or maybe const-pointer / const array of characters?
A "C string" is an array of characters that ends with a 0 (null character) byte. The array, not any pointer, is the string. Thus, any terminal subarray of a C string is also a C string. Pointers of type char *
(or const char *
, etc.) are often thought of as pointers to strings, but they're actually pointers to an element of a string, usually treated as a pointer to the initial element of a string.
Const or non-const array of characters, terminated by a trailing 0 char. So all of the following are C strings:
char string_one[] = { 'H', 'e', 'l', 'l', 'o', 0 };
char string_two[] = "Hello"; // trailing 0 is automagically inserted by the compiler
const char *string_three = "Hello";