8

What really means by word "C-string" in C / C++? Pointer to char? Array of characters? Or maybe const-pointer / const array of characters?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

4 Answers4

8

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    @NikitaTrophimov No. R... said it very exactly: a C string is a `'\0'` terminated array of characters. `char*` and `char const*` are often used to refer to a C string, but they aren't a C string in themselves. – James Kanze Aug 30 '12 at 19:14
7

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";
  • So the arrays and pointers is the same thing and both can be treat as the C-string? – FrozenHeart Aug 30 '12 at 19:03
  • @NikitaTrophimov no, arrays and pointers are not the same, but in simpler cases, pointers can be treated as arrays an vice versa. Read here: http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/ –  Aug 30 '12 at 19:05
  • I know that pointers and arrays are really not the same thing. I want to know are there any difference between them in the meaning of C-string – FrozenHeart Aug 30 '12 at 19:07
  • @NikitaTrophimov Yes. You'll get different results for `sizeof(str)` depending on how you declare `str` (as an array or as a pointer). –  Aug 30 '12 at 19:08
  • I know it. My question is can i call pointers to chars C-strings or not? Maybe arrays only? – FrozenHeart Aug 30 '12 at 19:10
  • 1
    @NikitaTrophimov Call zero-terminated char pointers and zero-terminated char arrays C strings. –  Aug 30 '12 at 19:10
3

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • @Mahesh, It's just as valid to change things by dereferencing a pointer (maybe you're going through character by character and need to change something). `const char *` should definitely be used when pointing to literals, though. Perhaps that's what crossed your mind? – chris Aug 30 '12 at 18:42
3

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'.

md5
  • 23,373
  • 3
  • 44
  • 93