5
char arr[3];
arr="hi";// ERROR
cin>>arr;// and at runtime I type hi, which works fine.

1)can someone explain to me why?

2)and what's exactly is the type of "hi", I know it's called literal string. but is it just an array of chars too?

3) isn't cin>>arr; will be just like assign arr to what you type at runtime?

0

2 Answers 2

6

Arrays in C++ are not actual types, just a structured representation of a series of values, and not pointers if you should find that anywhere (they decay into pointers). You can't use them like you would use other types, including assignment. The choice was to either add lots of support for arrays, or to keep them as simple and fast as possible. The latter was chosen, which is one of the distinctions C++ has from some other languages.

To copy an array, copy each element one at a time.

In C++11, there is an STL container std::array. It was designed to fit in as a plain array with operator overloading, as well as relating to the rest of the STL.

A better alternative is std::string. It incorporates the behaviour you want and more, and is specifically designed for holding arrays of characters.

"hi" is, as Konrad Rudolph points out, a const char [3].

As for cining a raw array, it is not possible by standard means because there is no overload provided for cin with arrays. It is possible to create your own overload though. However, I'm not sure how you would account for the different sizes of arrays that get passed unless you define it for a container that knows its size instead of a raw array.

Sign up to request clarification or add additional context in comments.

9 Comments

An even better alternative is std::string.
Oops, yeah forgot it was an array of characters ^_^
+1 ;) This is one of the most important concepts to grasp when being new to C or C++. And yeah don't ever confuse an array with a pointer ;)
"hi" is not a const char*. It’s a const char[3]. Otherwise you couldn’t initialise an array with it.
@Alex Assignment to char[] copies the array so there’s no problem. Assignment to char* is technically invalid but C++ allows it for backwards compatibility reasons (C allows it). But every modern compiler should issue a warning for this code (at least all can be configured to do so), and as far as I remember it’s completely forbidden in C++11.
|
6

If you'd like, you can declare:

char array[] = "hi!";

Creates an array and 'initializes' it to 4 bytes long, "hi!"

char const *array2 = "hey!";

Creates a pointer to read-only memory, a string literal

array2 = array;

You can now use the array2 pointer to access array one. This is called pointer decay; array and array2 are not of the same type, even though they can cooperate here. An array of type char "decays" to a pointer-to of type char.

array = array2; // ERROR

An array is not a pointer. You're thinking like an array is a pointer, when really, it is pre-allocated. You're attempting to assign an address, but array[] already has one "hard-coded" when it was created, and it cannot be changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.