Arrays and pointers are quite different concepts. A pointer has nothing to do with an "unlimited array". Come on:
int lista[10][10];
Here you are declaring a two-dimensional array with 100 elements. The name of this array is lista
. When you type lista[4][2]
you access an element and this results in a int
. However, if you only type lista
in your expression, that is, when you try to access the array by name, it will fall into a pointer to the first element. The detail here is that it is not possible to represent the array type directly, it will decay into a pointer whenever requested. You can do the following then:
int lista[10][10];
int* ponteiro1 = lista; // ponteiro1 é &lista[0][0]
int* ponteiro2 = lista[3]; // ponteiro2 é &lista[3][0]
Already when you do the following
int** ponteiro;
You are only creating a pointer that points to a pointer that points to a int
. There are no arrays here. The ponteiro[2][3]
notation that is possible represents a bit of algebra with pointers, being equivalent to *(*(ponteiro+2)+3)
. This may not even result in a valid memory location.
Yet another possibility:
int* lista2[10]; // array de ponteiros
Here you have a simple one-dimensional list whose element is a int*
pointer.
The last notation of your question ( tipo nova_variavel[limite] ponteiro
) would aim to create a pointer named nova_variavel
that points to an array of limite
elements of type tipo
? In this case your definition is written like this:
int (*variavel)[10]; // ponteiro de arrays
Not so intuitive, maybe. An example:
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int (*ponteiro_para_array)[10] = &array;
cout << (*ponteiro_para_array)[5] << endl; // mostra 6
}
One way to escape these complications when declaring a variable with an unusual type is to name each part of its type. For example (C ++ 11):
using int10 = int[10]; // Cria um alias para a array de 10 ints
int10* variavel; // Exatamente o mesmo que a declaração anterior