+ 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??
7 Answers
+ 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)
+ 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
+ 2
ă Nicko12 ă thanks a lot (ââ˘á´â˘â)
+ 2
ă Nicko12 ă Thanks so much now I get it.・ââżâ・. ŕźź 㤠ââżâ ŕź˝ă¤
+ 2
@ă Nicko12 ă
Both arr1 and arr2 now "point" to same array.
+ 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.
+ 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;
}