0

Hello to anyone reading this. I am new to C++. I have some conceptual questions about C-strings. I learned that C-string acts like an array from another stack discussion, but I am confused by the following:

I know that in C++, for arrays:

int int_arr[] = {1, 2, 3, 4, 5}; 
cout << int_arr << endl;        // Address of first element of the array, prints an address
cout << *int_arr << endl;       // Content of first element of the array, prints 1
// int_arr acts like &(int_arr[0]) in assignment, comparison, etc.

I expected the same behavior for C-strings, but:

char c_str[] = "Hello";
cout << c_str << endl;          // Prints "Hello"
cout << *c_str << endl;         // Prints "H"

This contradicts the behavior of the array.
The following setup has the same behavior as a C-string:

char *c_string = "Hello";
cout << c_string << endl;      // Prints "Hello"
cout << *c_string << endl;     // Prints "H"

There are two questions here:

  • Is it correct that C-string does not act like an array, and I am supposed to remember C-string with its own behavior? If it is an array, why does it act differently in the above example of arrays, not returning the address but the whole string?
  • Is the third code section simply another way of setting up a C-string? It only works when it is a char*. (Resolved)
Pictsit
  • 15
  • 4
  • 5
    "Acts like" nothing, a C-style string *is* an array. It's an array of characters. The fact that there's an overload of `std::cout`'s operator `<<` for it has nothing to do with whether or not it's an array. – Silvio Mayolo Aug 11 '22 at 01:54
  • 1
    I think you're confusing C++ being able to use C style arrays with C++ features like `std::vector` or specialized containers like `std::string`. – tadman Aug 11 '22 at 01:57
  • 1
    This is not **another way of setting up a C-string**. This is setting a pointer to point an array, the first char in a char array. – 273K Aug 11 '22 at 01:58
  • 1
    Hint: `c_str` is a `char*` which has a specialized output operator. `*c_str` is the same as `c_str[0]` which is of course just `char`, of course with `const` applied. – tadman Aug 11 '22 at 01:59
  • 1
    Note that `char *c_string = "Hello";` is deprecated (because having a non-const pointer to an array of chars that is located in non-writable memory is asking for trouble). The proper form would be `const char *c_string = "Hello";` – Jeremy Friesner Aug 11 '22 at 01:59
  • @273K Thank you! I understand how the third section works now. – Pictsit Aug 11 '22 at 02:00
  • 1
    Array of characters with a sentinel nul character marking the end of the "used" part of the array and specialized support for treating it more like a string. But note that C++ also has std::string for managing strings. – Avi Berger Aug 11 '22 at 02:01
  • 1
    **If it is an array, why does it act differently in the above example?** It's time to start studying function overloads. – 273K Aug 11 '22 at 02:03
  • @273K I learned about overflow just now to get my answer to my own question. I want to thank you for pointing me on the right path! – Pictsit Aug 11 '22 at 02:12
  • @SilvioMayolo I learned about overflow just now to get my answer to my own question. I want to thank you for pointing me on the right path! – Pictsit Aug 11 '22 at 02:13

0 Answers0