C ++: copy array

Is it possible to do something like this in C ++ (can't check it right now)?

int myarray[10] = {111,222,333,444,555,666,777,888,999,1234};

void functioncc()
{
 int temparray = myarray;
 for(int x=0; x<temparray.length; x++){
    .... do something
 }

}

And maybe this (but I don't think so):

int array1[5] = {0,1,2,3,4,5,6,7,8,9};
int array2[5] = {9,8,7,6,5,4,3,2,1,0};

void functioncc(int arid)
{
  temparray[10] = "array"+arid;
  ........

}

I can do such things in JavaScript, but as I said, don’t think that this is possible in C ++.

Thank you for your time.

+3
source share
3 answers

Of course.

int myarray[] = {111,222,333,444,555,666,777,888,999,1234};

void function() {
    std::vector<int> temparray(std::begin(myarray), std::end(myarray));
}

Note that using static non-constant variables in this way really looks from top to bottom, and if you pass them to other functions, you will also need to pass an "end" pointer.

However, C ++ is so different from Javascript, seriously, just don’t worry. If you need to encode C ++, get a real C ++ resource and learn it. The syntax of the main material is ONLY a thing.

+3
#include <cstring>

int temparray[10] ;
memcpy (temparray, myarray, sizeof (myarray)) ;
+8

Both cases are impossible. You must either put the length of the array as an argument (be aware of this) or put some kind of “terminator” inside the array as the last element. (IE in a pointer array places a NULL pointer at the end of the array)

+1
source

Source: https://habr.com/ru/post/1762241/


All Articles