As we studied the arrays, 2 questions arose:

one.

#include <iostream> using std::cout; using std::cin; int main (void) { setlocale(LC_ALL, "ru"); int a[5]; cout << a << "\n" << &a << "\n"; system("pause"); } 

Why is the same address printed? How to read, а - a pointer to the first element of the array. We print the address of this first element, and then the address of the pointer to it, but the address is the same. Can you clarify the situation?

2. Record

 int a[5] 

and

 int (*a)[5] 

What does the second example mean, how to work with it (with the help of new) and what is its difference from the first one.

    1 answer 1

    1. a is not a "pointer to the first element of an array". a is the array itself . The array is not a pointer.

      However, in some (quite numerous) contexts, the value of the array type is implicitly automatically converted to the value of the pointer type to array element type. This pointer points to the first (zero) element of the array. Hence the illusion that the array is a pointer.

      But such a transformation is not always done. The unary built-in operator & is just an example of an exception : the operand of the unary & remains an array. Therefore, in your case, &a is not a "pointer address", as you incorrectly assumed, but the address of the entire array a . It is of type int (*)[5] .

      At the same time, in your example, a is simply subjected to this transformation, i.e. gives a pointer to the first element of the array. It is int * .

      It is not surprising that these addresses are numerically the same. The first element of the array begins in memory exactly where the entire array begins.

    2. The second example is a pointer to an array. It is not fundamentally different from other pointers, and it should be “worked” with it in the same way as with any other pointer type. Apply the unary operator * to this pointer - and you will get access to the specified array, which will behave in the same way as any other array.

    • Thank you very much. Angry with the fact that they do not immediately give a complete, detailed description of the topic (What they give is: “Here, Lesha, there is such a thing, an array is called. It has a length, and elements can be addressed by index.” They show the syntax of description, reference. That's all. ), and then you pass it, you think - everything, I know, you go further, and then it turns out that you knew this topic in half. Well, if the material was very difficult, not to give it immediately, so no ... - Alex
    • This is not quite true. You just have not had time to study in detail ... For each topic there are textbooks where everything is described in detail - AR Hovsepyan
    • Did not quite understand "... did not have time to study ...". "Is"? Maybe. Somewhere, maybe there is. - Alex