Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign a char array to a literal string - c++

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?

like image 930
Alex Dannk Avatar asked Apr 21 '12 23:04

Alex Dannk


People also ask

Is it possible to modify a string literal in C?

The only difference is that you cannot modify string literals, whereas you can modify arrays. Functions that take a C-style string will be just as happy to accept string literals unless they modify the string (in which case your program will crash).

How do you initialize a string literal?

The best way to declare a string literal in your code is to use array notation, like this: char string[] = "I am some sort of interesting string. \n"; This type of declaration is 100 percent okey-doke.


2 Answers

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.

like image 62
chris Avatar answered Oct 20 '22 15:10

chris


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.

like image 36
std''OrgnlDave Avatar answered Oct 20 '22 15:10

std''OrgnlDave