Pointers logic

5

In C ++, you can do the following:

int variavel[10][10];
int** variavel;

But when I want to create an array 2d, where can I have the first part "unlimited" and the second with limit?

tipo ponteiro nova_variavel[limite]

And the "reverse"?

tipo nova_variavel[limite] ponteiro

For example:

int* a[5]; //Ponteiro de arrays

Or

std::vector<int*> a; //Array de ponteiros

char* A[6];
A[1] = "Hello,";
A[2] = " World";
std::array<char*,2> a;
a[1] = "Hello,";
a[2] = "World!"; // <- Repare 1 caractere a mais. É pouco, mas pode fazer diferença.
    
asked by anonymous 08.02.2014 / 15:55

2 answers

3

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
    
08.02.2014 / 16:19
0

I think having an "array of pointers" makes sense, but an "arrays pointer" does not. If you declare:

int *variavel[10];

What you are saying is: "allocate memory in the stack for 10 pointers to integer, and give me the address of the first one." In that case, the second part is "unlimited" in a way - since you can make each of these pointers reference an array of any size (provided you allocate memory for each of them as well, of course ).

The reverse side (which I'm not going to give an example, as I know it does not exist) would be like saying: "create a pointer that only references arrays of integers of size 10 ". That is, you are imposing an constraint constraint on the objects that that pointer can refer to, and this is not something supported by the C ++ language (or by any language I know). For the only restriction you can put on a pointer is the specification of your type - and nothing about your content .

    
08.02.2014 / 16:12