+ 1

Can we pass values of array1 to array2?

I have this example.. double array1[3]={1,2,3} and double array2[3], why we can't simply use this statement... array1=array2??

8th Dec 2020, 2:32 PM
🏐Volley
🏐Volley - avatar
7 ответов
+ 3
i think because array name is constant,so you cant assign it to other array u can use pointer or copy method std::copy(array1, array1 + 3, array2);(assign array 1 to array 2)
8th Dec 2020, 3:09 PM
durian
durian - avatar
+ 2
I used pointers but I think there are other ways to do it too or maybe none, I just dont know cause Im also still learning C++. But I Hope this helped you. https://code.sololearn.com/cdnv79IMLHl0/?ref=app
8th Dec 2020, 2:55 PM
noteve
noteve - avatar
+ 2
《 Nicko12 》 thanks a lot (◍•ᴗ•◍)
8th Dec 2020, 2:57 PM
🏐Volley
🏐Volley - avatar
+ 2
《 Nicko12 》 Thanks so much now I get it.。◕‿◕。. ༼ つ ◕‿◕ ༽つ
8th Dec 2020, 3:01 PM
🏐Volley
🏐Volley - avatar
+ 2
@《 Nicko12 》 Both arr1 and arr2 now "point" to same array.
8th Dec 2020, 4:38 PM
rodwynnejones
rodwynnejones - avatar
+ 1
But Ill explain what Ive found If you do this: cout << arr; //The output will be the address of the 'arr' in the memory, thats why I thought of using pointers.
8th Dec 2020, 2:58 PM
noteve
noteve - avatar
+ 1
🏐Volley not with c-style array, but with c++ array you can if they have same type and length. e.g. #include <iostream> #include <array> int main() { std::array<int, 3> m = { 1, 2, 3 }; std::array<int, 3> mm = { 4 }; mm = m; // same type and size of both array for (auto& i : mm) { std::cout << i << '\n'; } return 0; }
8th Dec 2020, 3:32 PM
Flash