YouTip LogoYouTip

Cpp Array Of Pointers

## C++ Array of Pointers In C++, pointers are variables that store the memory addresses of other variables. Just as you can create arrays of integers, floats, or characters, you can also create an **array of pointers**. This means each element of the array is a pointer pointing to a memory location. Before diving into the concept of pointer arrays, let's first look at a basic example of a standard integer array. ### Basic Integer Array Example ```cpp #include using namespace std; const int MAX = 3; int main () { int var = {10, 100, 200}; for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << var << endl; } return 0; } ``` **Output:** ```text Value of var = 10 Value of var = 100 Value of var = 200 ``` --- ## Syntax and Declaration There are situations where you want an array to store pointers pointing to `int`, `char`, or other data types. The syntax to declare an array of pointers is as follows: ```cpp type *array_name; ``` For example, to declare an array of pointers to integers: ```cpp int *ptr; ``` Here, `ptr` is declared as an array of `MAX` integer pointers. Each element in `ptr` (such as `ptr`, `ptr`, etc.) is a pointer that can hold the address of an `int` variable. --- ## Code Examples ### Example 1: Array of Integer Pointers The following program demonstrates how to store the addresses of three integer variables in an array of pointers and access their values using dereferencing (`*`). ```cpp #include using namespace std; const int MAX = 3; int main () { int var = {10, 100, 200}; int *ptr; // Assign the address of each integer to the pointer array for (int i = 0; i < MAX; i++) { ptr = &var; } // Access the values using the pointer array for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // Dereferencing the pointer } return 0; } ``` **Output:** ```text Value of var = 10 Value of var = 100 Value of var = 200 ``` --- ### Example 2: Array of Character Pointers (Array of Strings) An array of character pointers (`const char* []`) is a highly efficient way to store a list of strings. Each element of the array points to the first character of a string literal. ```cpp #include using namespace std; const int MAX = 4; int main () { // Array of pointers to constant characters const char *names = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", }; for (int i = 0; i < MAX; i++) { cout << "Value of names[" << i << "] = "; cout << names << endl; } return 0; } ``` **Output:** ```text Value of names = Zara Ali Value of names = Hina Ali Value of names = Nuha Ali Value of names = Sara Ali ``` --- ## Key Considerations 1. **Operator Precedence:** In the declaration `int *ptr`, the subscript operator `[]` has higher precedence than the dereference operator `*`. Therefore, `ptr` is first associated with `` (making it an array), and then with `*` (making it an array of pointers). * Contrast this with a **pointer to an array**: `int (*ptr)`. Here, the parentheses force `ptr` to be a single pointer pointing to an entire array of `MAX` integers. 2. **Memory Efficiency:** Using an array of character pointers (`const char *names[]`) to store strings is more memory-efficient than a 2D character array (`char names`). In a 2D array, every row must have the same size (padding shorter strings with null terminators). With an array of pointers, each pointer points to a string of arbitrary length, eliminating wasted space. 3. **Const Correctness:** When pointing to string literals (e.g., `"Zara Ali"`), always use `const char*` instead of `char*`. String literals are stored in read-only memory, and attempting to modify them via a non-const pointer will trigger undefined behavior.
← Cpp Pointer To PointerCpp Pointers Vs Arrays β†’