In this section, you will learn how pointers and arrays are related to each other. Before that, you should have knowledge on the following topics in C.
Pointers and arrays are strongly related to each other. In general, the name of the array is a pointer itself, it points to the address of the first element in an array. For example, if an array of name score[] is created then the name (score) contains the address of a first element. Through this, we can access other elements.
Consider the following:
1 2 3 4 | double *ptr; double arr[10]; ptr = arr; |
In the above example, the variable arr will provide the base address, which is a constant pointer pointing to the first element of the array that is to arr[0]. And hence the arr will contain the address of arr[0]. Thus, the above program fragment assigns ptr with the address of the arr.
Once the address of the first element is stored in the pointer ‘p’, we can access other elements of array elements using *p, *(p+1), *(p+2)
, and so on.
Let us see it in a C++ program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; int main() { int *ptr; int arr[] = { 10, 20, 30, 40, 50 }; //assigning the address of first element to pointer ptr = arr; for (int i = 0; i < 5; i++) { cout << "Address: " << ptr << endl; cout << "Value: " << *ptr << endl; ptr++; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 | Address: 0x7ffea05a98f0 Value: 10 Address: 0x7ffea05a98f4 Value: 20 Address: 0x7ffea05a98f8 Value: 30 Address: 0x7ffea05a98fc Value: 40 Address: 0x7ffea05a9900 Value: 50 |
C++ Array of Pointers
An array of pointers refers to the array whose elements are pointer type that stores a pointer to an integer or char or any other data types in C++. These variables of pointers point to some other element.
The declaration of array of pointers is done in the following way:
1 | int *ptr[10]; |
In the above declaration, an array of pointer named ptr
is created that that allocates 10 integer pointers in memory.
We can also access or assign the variable through array of pointers such as:
1 2 3 4 | int x; // variable declaration. ptr[3] = &x; //assigning the address of x to 4th element *ptr[2]; accessing the 3rd element |
Let us see it in a C++ program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; int main() { int number[5] = { 10, 20, 30, 40, 50 }; int *ptr[5]; // assign the address of number element tp ptr. for (int i = 0; i < 5; i++) { ptr[i] = &number[i]; } //Displaying the value in ptr for (int i = 0; i < 5; i++) { cout << "number[" << i << "] = "; cout << *ptr[i] << endl; } return 0; } |
Output:
1 2 3 4 5 | number[0] = 10 number[1] = 20 number[2] = 30 number[3] = 40 number[4] = 50 |
Array of Pointers to character
We can use array of pointers to store a list of string in C++. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; int main() { const char *names[5] = { "Leanord Drest", "Zelda", "Tony Star", "Peter", "Steve" }; for (int i = 0; i < 5; i++) { cout << "names[" << i << "] : " << names[i] << endl; } return 0; } |
Output: After execution, you will get the following result.
1 2 3 4 5 | names[0] : Leanord Drest names[1] : Zelda names[2] : Tony Star names[3] : Peter names[4] : Steve |