Home c++ pointer to the C++ array

pointer to the C++ array

Author

Date

Category

Let we have a static array:

short tell [20];

where:

  1. tell – address of the first element of the array (2-byte block).
  2. & amp; Tell – address of the whole array (40-byte block).

and there is the following design, indicating an array of 20 types of type of type:

short (* PAS) [20] = & amp; Tell

PAS variable data type is Type: Short (*) [20] . Actually, the question is how to create a variable of this type, and to allocate her memory using the new operation?

An example is taken from the book: Stephen Prat – C++ programming language (6 edition). P. 182.


Answer 1, Authority 100%

If you have an ad array of the type

t a [n];

Where T is some type, and n is the number of elements in the array, the pointer to the first element of the array will have the type T * . For example

t * p = a;

After this definition, the p pointer indicates the first element of the array A .

To highlight dynamically memory for an array, similar to the array defined above, you can record

t * p = new t [n];

The element of the array has the type t , and p as above shows on the first element of a dynamically selected mensally array ..

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

typeedef short t [20];

Then previously shown ads for the pointer can be recorded as

t * p = & amp; a;

and

t * p = new t [1];

If you return to the original type Short [20] , we get

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

and

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

The last sentence means that an array of one element is distinguished (you can highlight an array of an arbitrary number of items according to your task), the elements of which in turn are arrays from 20 SHORT elements.

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

so if you, for example, announce the pointer as

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

Where T is equivalent to short [20] , then after use, for example, the increment to this pointer

++ p;

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

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

# include & lt; iostream & gt;
#InClude & lt; Iomanip & gt;
#Include & lt; algorithm & gt;
#Include & lt; Numeric & GT;
#Include & lt; Iterator & GT;
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 & LT; & LT; STD :: SETW (2) & LT; & LT; x & lt; & lt; '';
  STD :: COUT & LT; & LT; STD :: ENDL;
  Short (* p) [N] = Create_2D_Array (1);
  STD :: IOTA (STD :: REVERSE_ITERATOR & LT; SHORT * & GT; (* P + N),
        STD :: REVERSE_ITERATOR & LT; SHORT * & GT; (* P), 1); 
For (Short X: * p) STD :: COUT & LT; & LT; STD :: SETW (2) & LT; & LT; x & lt; & lt; '';
  STD :: COUT & LT; & LT; STD :: ENDL;
  STD :: COUT & LT; & LT; STD :: ENDL;
  STD :: SWAP (A, * P);
  For (Short X: a) STD :: COUT & LT; & LT; STD :: SETW (2) & LT; & LT; x & lt; & lt; '';
  STD :: COUT & LT; & LT; STD :: ENDL;
  For (Short X: * p) STD :: COUT & LT; & LT; STD :: SETW (2) & LT; & LT; x & lt; & lt; '';
  STD :: COUT & LT; & LT; STD :: ENDL;
  delete [] p;
  Return 0;
}

Program output on 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

Answer 2, Authority 36%

Actually and Tell , and & amp; Tell [0] , and & amp; Tell is all the same essence The address from which the memory unit begins highlighted for an array …

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

Here we describe the type of array of 20 Short , and create it dynamically, returning the pointer to the beginning of the block with it. We work with him, as with a conventional array, like

for (short i = 0; i & lt; 20; ++ i)
{
  a [i] = i;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions