#include <iostream> using namespace std; int main() { int b[10]; int ( *a )[ 10 ]; // эта a = b; // error a[ 0 ] = new int; // error a = new int; // error } I am trying to use the selection method to understand what it is and how it is used .
#include <iostream> using namespace std; int main() { int b[10]; int ( *a )[ 10 ]; // эта a = b; // error a[ 0 ] = new int; // error a = new int; // error } I am trying to use the selection method to understand what it is and how it is used .
To make it easier to understand the record
int ( *a )[ 10 ]; you can enter an alias definition for the array
typedef int T[10]; T *a; that is, objects that a pointer can address are integer arrays of ten elements.
If you want, for example, that this pointer addresses such an array, you can write
typedef int T[10]; T b; T *a; a = &b; Then the expression *a or a[0] is an array b
For example, you can write
typedef int T[10]; T b; T *a; a = &b; for ( int i = 0; i < 10; i++ ) a[0][i] = i; If you want to dynamically allocate an array, then you should write
typedef int T[10]; T *a; a = new T[1]; This is equivalent to the following code snippet.
int ( *a )[10]; a = new int[1][10]; That is, such a pointer is usually used when working with two-dimensional arrays. For example,
#include <iostream> int main() { const size_t M = 2; const size_t N = 10; int b[M][N] = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 } }; for ( int ( *a )[N] = b; a != b + M; ++a ) { for ( int x : *a ) std::cout << x << ' '; std::cout << std::endl; } } Console output
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0 for (auto&& row : b) for (auto x : row) cout << x << ' '; - jfsFormally, this is a pointer to an array of 10 int .
So you need to assign as
a = &b; If you want to use to assign an element - then
(*a)[0] = 12; // Присваивание b[0] = 12 If you mean an array of arrays of 10 elements,
a[0][3] = 15; // Присваивание третьему элементу первого массива // по адресу a значение 15 - то же, что и b[3] = 15; Well, and the last ... for example, like this:
using arr = int[10]; a = new arr[1]; vector . Basically, here is a two-dimensional array :) We declare something like a = new arr[5] down in the answer, and we get a two-dimensional array ... - HarrySource: https://ru.stackoverflow.com/questions/628354/
All Articles