C++ arrays are used to store a collection of homogeneous data types. The array's elements are stored in contiguous memory locations and can be accessed by their index values, which start at 0. C++ also has the concept of pointers, which is a variable that stores the memory address of another variable. Arrays and pointers can be combined by creating an array of pointers to store the memory addresses of other arrays.
An example of using an array of pointers in C++ would be to create a dynamic array of strings. This can be done by declaring an array of string pointers with a specific size. Each element in the array can be dynamically allocated using the "new" keyword, and the pointer to the memory location of the string can be stored in the array of pointers.
```c++
#include
#include
using namespace std;
int main() {
// declare an array of pointers
string* names[3];
// dynamically allocate memory for each string
names[0] = new string("John");
names[1] = new string("Sarah");
names[2] = new string("Mike");
// access the strings using the array of pointers
for (int i = 0; i < 3; i++) {
cout << *names[i] << endl;
}
// free the memory
for (int i = 0; i < 3; i++) {
delete names[i];
}
return 0;
}
```
This example demonstrates how to use an array of pointers to store and access strings dynamically. The "string" library is used to define the data type of the strings. The "new" keyword is used to allocate memory for each string, and the "*" operator is used to dereference the pointers and access the strings' values. Finally, the "delete" keyword is used to free the dynamically allocated memory.
The package library used in this example is the standard C++ library, which provides the "string" data type and the new and delete keywords for dynamic memory management.
C++ (Cpp) Ptr - 30 examples found. These are the top rated real world C++ (Cpp) examples of array::Ptr extracted from open source projects. You can rate examples to help us improve the quality of examples.