1
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <algorithm>
    #include <iomanip>
    #include <cstring>
    using namespace std;

    int main() {
        const int size = 15;
        char array1[size];
        char array2[size] = "four";
        cout << array2[3]='\0' << endl;
        return 0;
    }

Hello, I am new to C++, I am learning the C-string, my question is why
array2[3] ='\0' can not be use inside the cout, the error I get is:

 reference to overloaded function could not be resolved.

It works fine if it's outside of cout. thank you :)

Waqar
  • 8,558
  • 4
  • 35
  • 43
newLearner
  • 19
  • 4
  • 5
    Shift operator has higher precedence than assignment. That line is interpreted as `(cout << array2[3]) = ('\0' << endl);` which is nonsense. If you really want to combine assignment and output for some reason, enclose the assignment in parentheses. – Igor Tandetnik Jul 25 '20 at 20:49
  • 2
    ... But don't combine output and assignment. Clever code is often opaque code, and you or someone else will damage it later during maintenance. – cdhowie Jul 25 '20 at 20:53
  • thank you, another question, if i only put down int size =15, without using keyword const, why there will be error on array2? – newLearner Jul 25 '20 at 20:57
  • 1
    that's a separate question. one question per posting--this keeps it easy for future people to find relevant questions. – rsjaffe Jul 25 '20 at 21:02
  • It appears you could use a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Eljay Jul 25 '20 at 21:05
  • Because C++ requires array size to be fixed at compile-time (i.e. be a compile-time constant). `const int size = 15;` counts as a compile-time constant, and `int size = 15;` is not even a constant. – HolyBlackCat Jul 25 '20 at 21:06

0 Answers0