A multidimensional array in C ++, a secret for me :(

I have many problems understanding multidimensional arrays. Let the array be

x[2]; // now x is constant pointer to first element i.e x name is address of x[0]

Now a two-dimensional array:

x[2][5]; // here x is address of x[0] which contains the address of first element of array x[][0];

Now pointer

int(*y)[5]; 

is a pointer to an array of 5 integers. How can I write y = x?

Now I practiced this to figure it out in VS, which is here, and my main question is in the image:

http://img184.imagevenue.com/img.php?image=96382_first_122_1120lo.jpg

Please answer the question conceptually; how C ++ stores a multidimensional array, etc.

I will be very grateful for any help :)

+5
source share
3 answers

, , .

, , . . - , . , int x[2], , . , .

, C/++ . , x, &x &x[0]. , , , .

- . , , . C/++, , , [] (), *, , , ( ):

int              x      [                2]         [           5];
6. "of type int" 1. "x" 2. "is an array" 3. "of 2". 4. "arrays" 5. "of 5 elements"

, C/++ , , , . x, &x &x[0] . , x[0] , , int. &x[0] &x[0][0]. x[0] , x[0] - .

, , , . x 5 ints, &x[0], x. x[0] x[0], int, &x[0][0]. 4 :

  int x[2][5];
  int (*y1)[5] = x;
  int *y2 = x[0];
  int (*y3)[5] = &x[0];
  int *y4 = &x[0][0];
  printf("%p %p %p %p\n", y1, y2, y3, y4);

, . , . C/++, "" , . int x[2][5], , 2 5 , 2 , , " ", shybovycha .

, . "" :

  • . -.
  • . , , malloc() new [].
  • - .

, (, int **y), , , .

+7

: . . x[2][5] :

x[0][0] x[0][1] x[0][2] x[0][3] x[0][4]

x[1][0] x[1][1] x[1][2] x[1][3] x[1][4]

: x[0] - 2x5, int *y = x[0] int *y = x[1] y.

int (*y) = x, x[0][0] y (int y int, int (*y) ).

UPD:. int **x. , . , x, x[0] x[0][0] . Pointer name means its address. Multi-dimensional array is a pointer to pointer. Its name means address of pointer it pointing on which means address of the last pointer' first element. ( ).

+6

, x [0] for the first array of your 2d array, and x [1] is a pointer to your second array. So, just think about x [0], as this is the name of your 1st array. Adding another pair of brackets (for example, x [0] [0]) will return you the value of the element. And the address of the first element will be & x [0] [0] or just x [0].

+3
source

All Articles