Assigning one array to another C ++ array

Hi, I am new to C ++, can someone explain this to me

char a[]="Hello"; char b[]=a; // is not legal 

then,

 char a[]="Hello"; char* b=a; // is legal 

If the array cannot be copied or assigned to another array, why is it so that it can be passed as a parameter, where a copy of the passed value is always executed in the method

 void copy(char[] a){....} char[] a="Hello"; copy(a); 
+7
c ++ arrays
source share
2 answers

It does not copy the array; he turns it into a pointer. If you change it, you yourself will see:

 void f(int x[]) { x[0]=7; } ... int tst[] = {1,2,3}; f(tst); // tst[0] now equals 7 

If you need to copy an array, use std::copy :

 int a1[] = {1,2,3}; int a2[3]; std::copy(std::begin(a1), std::end(a1), std::begin(a2)); 

If you do this, you can use std::array .

+7
source share

The array is silently (implicitly) converted to a pointer in the function declaration, and the pointer is copied. Of course, the copied pointer points to the same place as the original, so you can change the data in the original array using the copied pointer in your function.

0
source share

All Articles