Suppose we have a static array:

short tell[20]; 

Where:

  1. tell is the address of the first element of the array (2-byte block).
  2. &tell - the address of the whole array (40-byte block).

And there is the following construct, pointing to an array of 20 short elements:

 short (*pas)[20] = &tell 

The resulting data type for the pas variable is type: short (*)[20] . Actually the question is how to create a variable of this type and allocate memory for it using the new operation?

The example is taken from the book: Stephen Prath - C ++ Programming Language (6th edition). Page 182.

    2 answers 2

    If you have an array declaration of the form

     T a[N]; 

    where T is some type and N is the number of elements in the array, then the pointer to the first element of the array will be of type T * . for example

     T *p = a; 

    After this definition, the pointer p points to the first element of the array a .

    To allocate memory dynamically for an array similar to the array defined above, you can write

     T *p = new T[N]; 

    Here, the array element is of type T , and p as above, shows the first element of the dynamically allocated nameless array.

    Now imagine that T is an alias for the type of short[20] , for example

     typedef short T[20]; 

    Then previously displayed ads for the pointer can be written as

     T *p = &a; 

    and

     T *p = new T[1]; 

    If we again return to the original type of short[20] , we get

     short( *p )[20] = &a; 

    and

     short( *p )[20] = new short[1][20]; 

    The last sentence means that an array is selected from one element (you can select an array from an arbitrary number of elements in accordance with your task), whose elements in turn are arrays of 20 short elements.

    Keep in mind that when so-called pointer arithmetic is used, the pointer value changes to a multiple of sizeof( T )

    So if, for example, you declare a pointer as

     short( *p )[20] = &a; 

    where T equivalent to short[20] , then after applying, for example, an increment to this pointer

     ++p; 

    the pointer will contain the address immediately after the last element of the array a .

    Well, and finally, an example of working with such a pointer.

     #include <iostream> #include <iomanip> #include <algorithm> #include <numeric> #include <iterator> const size_t N = 20; short ( * create_2D_array( size_t n ) )[N] { short ( *p )[N] = new short[n][N]; return p; } int main() { short a[N]; std::iota( std::begin( a ), std::end( a ), 1 ); for ( short x : a ) std::cout << std::setw( 2 ) << x << ' '; std::cout << std::endl; short ( *p )[N] = create_2D_array( 1 ); std::iota( std::reverse_iterator<short *>( *p + N ), std::reverse_iterator<short *>( *p ), 1 ); for ( short x : *p ) std::cout << std::setw( 2 ) << x << ' '; std::cout << std::endl; std::cout << std::endl; std::swap( a, *p ); for ( short x : a ) std::cout << std::setw( 2 ) << x << ' '; std::cout << std::endl; for ( short x : *p ) std::cout << std::setw( 2 ) << x << ' '; std::cout << std::endl; delete [] p; return 0; } 

    Output of the program to the console

      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
    • Thank you very much, quite a qualitative explanation of exactly what was required. - IgrikXD

    Actually, tell , and &tell[0] , and &tell is all the same entity, the address from which the block of memory assigned to the array begins ...

     typedef short array[20]; short * a = new array; 

    Here we describe the type of an array of 20 short , and create it dynamically, returning a pointer to the beginning of the block with it. We work with it as with a regular array, such as

     for(short i = 0; i < 20; ++i) { a[i] = i; } 
    • Well, of course, when outputting the address, we get the same values. But, adding 1 to tell and & tell will give different results, namely, an increase in address values ​​by different values ​​will occur. - igrikxd
    • one
      The essence is one, and the types are different. - Abyx
    • @IXD Then I did not understand the question. Can I ask him in more detail? - Harry
    • @Harry The question is actually not how to create a normal dynamic array of type short, but the possibility of creating a variable of type short (*) [20] describing an array of 20 elements of type short. Where with an increment of the pointer by 1, we will get an increase in the address not by 2 units, but by 20. - IgrikXD
    • The article may clarify the situation: habrahabr.ru/post/251091 with the sub-item "Pointer to an array". However, my question of creating a variable of a certain type is not considered there. - IgrikXD