Reputation: 20726
What really means by word "C-string" in C / C++? Pointer to char? Array of characters? Or maybe const-pointer / const array of characters?
Upvotes: 9
Views: 14746
Reputation: 23699
According to the standard (C11 §7.1.1), a string is a contiguous sequence of characters terminated by and including the first null character, ie an array of character terminated by '\0'
.
Upvotes: 3
Reputation: 837926
A C-string is a series of characters that is terminated by a 0 byte, otherwise known as a null terminated string. It can be accessed either as an array (char[]
) or as a pointer to the first character (char *
).
In C++ there is another type of string called std::string
which does not need to be terminated by a 0 byte. The term C-string is often used by C++ programmers when they mean a null terminated string rather than the std::string
type.
Upvotes: 3
Reputation: 215193
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.
Upvotes: 8
Reputation:
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";
Upvotes: 7