Answer the question
In order to leave comments, you need to log in
Why is it necessary to specify the number of columns?
Why can't a two-dimensional array be passed to a function simply as a pointer
to an array? Why is the second dimension necessary?
So that the compiler allocates memory for a specific array?
Then why can you ignore the first dimension, if
you can write in the arguments like this: int test(int (*a)[5])
?
*a - this is a call to the address, to the address a , no?
So the name of the array in this context is not the same as the pointer??
Answer the question
In order to leave comments, you need to log in
In C++, two-dimensional arrays are not implemented at the language level, as is done, for example, in C#. Here we have only a little syntactic sugar, in the form of square brackets.
For example, all these expressions for referring to the third element of the array
a are equivalent and will be compiled:
know exactly how long they are (for addressing between rows) - we need to know the number of columns.
In case of dynamic memory allocation
const int ARRAY_SIZE = ...;
int **arr = new int*[ARRAY_SIZE];
for (int rown=0; rown<ARRAY_SIZE; ++rown) {
arr[rown] = new int[ARRAY_SIZE];
}
std::vector<T>
и std::valarray<T>
, на основе которых можно строить что-то вроде std::vector<std::vector<int>>
. Эти типы уже не голые указатели и несут в себе свойство size(), с помощью которого мы можем оценивать размерности.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question