C++ pointer to different array -


Assume that I have an array a and an array is b. Both are the same type and size but different values.

Now I make 3 or more pointers which indicate various elements in one, you can a [0] , a [4] And a [13] .

Now if I overwrite a with b a = b - where will the indicator indicate?

Indicators still point to their parent position in a , but the values ​​they call b

are those that are not fixed in C ++. Once you declare an array:

 < Code> int a [10];  

There is no way to overwrite it, only to change the value that is included in it. Specifically, you can not do this:

  int a [10], b [10]; A = B; // C ++ (or C) can not work  

If you dynamically create an array and assign points:

  int * A = new int [10]; Int * p = a + 1; Int * b = new int [10]; A = B;  

Then the Pointer P still points in the first array, but you have a memory leak.

In the case of a string with an array:

  straight s {int a [10]; }; SS1, S2; Int * p = s1.a + 1; S1 = s2;  

Then the indicator P still indicates the first structure of the array, but the array content will be overwritten with the array material from the second structure.


Comments