Matriz e ponteiros estão intimamente relacionados entre si. Em C++, o nome de um array é considerado um ponteiro, ou seja, o nome de um array contém o endereço de um elemento. C++ considera o nome do array como o endereço do primeiro elemento. Por exemplo, se criarmos um array, ou seja, marcas que contêm os 20 valores do tipo inteiro, então as marcas conterão o endereço do primeiro elemento, ou seja, marcas[0]. Portanto, podemos dizer que o nome do array (marcas) é um ponteiro que contém o endereço do primeiro elemento de um array.
Vamos entender esse cenário através de um exemplo.
#include using namespace std; int main() { int *ptr; // integer pointer declaration int marks[10]; // marks array declaration std::cout << 'Enter the elements of an array :' << std::endl; for(int i=0;i>marks[i]; } ptr=marks; // both marks and ptr pointing to the same element.. std::cout << 'The value of *ptr is :' <<*ptr<< std::endl; std::cout << 'the value of *marks is :' <<*marks<<std::endl; } < pre> <p>In the above code, we declare an integer pointer and an array of integer type. We assign the address of marks to the ptr by using the statement ptr=marks; it means that both the variables 'marks' and 'ptr' point to the same element, i.e., marks[0]. When we try to print the values of *ptr and *marks, then it comes out to be same. Hence, it is proved that the array name stores the address of the first element of an array.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers.webp" alt="C++ Array of Pointers"> <h3>Array of Pointers</h3> <p>An array of pointers is an array that consists of variables of pointer type, which means that the variable is a pointer addressing to some other element. Suppose we create an array of pointer holding 5 integer pointers; then its declaration would look like:</p> <pre> int *ptr[5]; // array of 5 integer pointer. </pre> <p>In the above declaration, we declare an array of pointer named as ptr, and it allocates 5 integer pointers in memory.</p> <p>The element of an array of a pointer can also be initialized by assigning the address of some other element. Let's observe this case through an example.</p> <pre> int a; // variable declaration. ptr[2] = &a; </pre> <p>In the above code, we are assigning the address of 'a' variable to the third element of an array 'ptr'.</p> <p>We can also retrieve the value of 'a' be dereferencing the pointer.</p> <pre> *ptr[2]; </pre> <p> <strong>Let's understand through an example.</strong> </p> <pre> #include using namespace std; int main() { int ptr1[5]; // integer array declaration int *ptr2[5]; // integer array of pointer declaration std::cout << 'Enter five numbers :' << std::endl; for(int i=0;i> ptr1[i]; } for(int i=0;i<5;i++) { ptr2[i]="&ptr1[i];" } printing the values of ptr1 array std::cout << 'the are' std::endl; for(int i="0;i<5;i++)" *ptr2[i] < pre> <p>In the above code, we declare an array of integer type and an array of integer pointers. We have defined the 'for' loop, which iterates through the elements of an array 'ptr1', and on each iteration, the address of element of ptr1 at index 'i' gets stored in the ptr2 at index 'i'.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-2.webp" alt="C++ Array of Pointers"> <p>Till now, we have learnt the array of pointers to an integer. Now, we will see how to create the array of pointers to strings.</p> <h3>Array of Pointer to Strings</h3> <p>An array of pointer to strings is an array of character pointers that holds the address of the first character of a string or we can say the base address of a string.</p> <p>The following are the differences between an array of pointers to string and two-dimensional array of characters:</p> <ul> <li>An array of pointers to string is more efficient than the two-dimensional array of characters in case of memory consumption because an array of pointer to strings consumes less memory than the two-dimensional array of characters to store the strings.</li> <li>In an array of pointers, the manipulation of strings is comparatively easier than in the case of 2d array. We can also easily change the position of the strings by using the pointers.</li> </ul> <p>Let's see how to declare the array of pointers to string.</p> <p>First, we declare the array of pointer to string:</p> <pre> char *names[5] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'}; </pre> <p>In the above code, we declared an array of pointer names as 'names' of size 5. In the above case, we have done the initialization at the time of declaration, so we do not need to mention the size of the array of a pointer. The above code can be re-written as:</p> <pre> char *names[ ] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'}; </pre> <p>In the above case, each element of the 'names' array is a string literal, and each string literal would hold the base address of the first character of a string. For example, names[0] contains the base address of 'john', names[1] contains the base address of 'Peter', and so on. It is not guaranteed that all the string literals will be stored in the contiguous memory location, but the characters of a string literal are stored in a contiguous memory location.</p> <p> <strong>Let's create a simple example.</strong> </p> <pre> #include using namespace std; int main() { char *names[5] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'}; for(int i=0;i<5;i++) { std::cout << names[i] std::endl; } return 0; < pre> <p>In the above code, we have declared an array of char pointer holding 5 string literals, and the first character of each string is holding the base address of the string.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-3.webp" alt="C++ Array of Pointers"> <hr></5;i++)></pre></5;i++)></pre></*ptr<<>
Na declaração acima, declaramos um array de ponteiros denominado ptr e aloca 5 ponteiros inteiros na memória.
O elemento de um array de um ponteiro também pode ser inicializado atribuindo o endereço de algum outro elemento. Vamos observar este caso através de um exemplo.
int a; // variable declaration. ptr[2] = &a;
No código acima, estamos atribuindo o endereço da variável 'a' ao terceiro elemento de um array 'ptr'.
Também podemos recuperar o valor de 'a' desreferenciando o ponteiro.
*ptr[2];
Vamos entender através de um exemplo.
#include using namespace std; int main() { int ptr1[5]; // integer array declaration int *ptr2[5]; // integer array of pointer declaration std::cout << 'Enter five numbers :' << std::endl; for(int i=0;i> ptr1[i]; } for(int i=0;i<5;i++) { ptr2[i]="&ptr1[i];" } printing the values of ptr1 array std::cout << \'the are\' std::endl; for(int i="0;i<5;i++)" *ptr2[i] < pre> <p>In the above code, we declare an array of integer type and an array of integer pointers. We have defined the 'for' loop, which iterates through the elements of an array 'ptr1', and on each iteration, the address of element of ptr1 at index 'i' gets stored in the ptr2 at index 'i'.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-2.webp" alt="C++ Array of Pointers"> <p>Till now, we have learnt the array of pointers to an integer. Now, we will see how to create the array of pointers to strings.</p> <h3>Array of Pointer to Strings</h3> <p>An array of pointer to strings is an array of character pointers that holds the address of the first character of a string or we can say the base address of a string.</p> <p>The following are the differences between an array of pointers to string and two-dimensional array of characters:</p> <ul> <li>An array of pointers to string is more efficient than the two-dimensional array of characters in case of memory consumption because an array of pointer to strings consumes less memory than the two-dimensional array of characters to store the strings.</li> <li>In an array of pointers, the manipulation of strings is comparatively easier than in the case of 2d array. We can also easily change the position of the strings by using the pointers.</li> </ul> <p>Let's see how to declare the array of pointers to string.</p> <p>First, we declare the array of pointer to string:</p> <pre> char *names[5] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'}; </pre> <p>In the above code, we declared an array of pointer names as 'names' of size 5. In the above case, we have done the initialization at the time of declaration, so we do not need to mention the size of the array of a pointer. The above code can be re-written as:</p> <pre> char *names[ ] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'}; </pre> <p>In the above case, each element of the 'names' array is a string literal, and each string literal would hold the base address of the first character of a string. For example, names[0] contains the base address of 'john', names[1] contains the base address of 'Peter', and so on. It is not guaranteed that all the string literals will be stored in the contiguous memory location, but the characters of a string literal are stored in a contiguous memory location.</p> <p> <strong>Let's create a simple example.</strong> </p> <pre> #include using namespace std; int main() { char *names[5] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'}; for(int i=0;i<5;i++) { std::cout << names[i] std::endl; } return 0; < pre> <p>In the above code, we have declared an array of char pointer holding 5 string literals, and the first character of each string is holding the base address of the string.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-3.webp" alt="C++ Array of Pointers"> <hr></5;i++)></pre></5;i++)>
No código acima, declaramos um array de nomes de ponteiros como 'nomes' de tamanho 5. No caso acima, fizemos a inicialização no momento da declaração, portanto não precisamos mencionar o tamanho do array de um ponteiro. O código acima pode ser reescrito como:
char *names[ ] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'};
No caso acima, cada elemento da matriz 'nomes' é uma string literal, e cada string literal conteria o endereço base do primeiro caractere de uma string. Por exemplo, nomes[0] contém o endereço base de 'joão', nomes[1] contém o endereço base de 'Pedro' e assim por diante. Não é garantido que todos os literais de string serão armazenados no local de memória contíguo, mas os caracteres de um literal de string serão armazenados em um local de memória contíguo.
Vamos criar um exemplo simples.
#include using namespace std; int main() { char *names[5] = {'john', 'Peter', 'Marco', 'Devin', 'Ronan'}; for(int i=0;i<5;i++) { std::cout << names[i] std::endl; } return 0; < pre> <p>In the above code, we have declared an array of char pointer holding 5 string literals, and the first character of each string is holding the base address of the string.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-3.webp" alt="C++ Array of Pointers"> <hr></5;i++)>5;i++)>*ptr<<>