POINTERS CONSTRUCT
POINTERS CONSTRUCT
Concept of pointers.
Pointers overview.
Pointers in C++
Pointers are variables that store the address of another variable. They play a vital role in
programming by enabling direct memory access, dynamic memory allocation, and
efficient parameter passing.
Concept of Pointers
Pointers Overview
• Declaration Syntax:
dataType *pointerName;
Example:
int x = 5;
int *p = &x; // Initialize pointer to address of 'x'
• Void Pointers: A pointer that can hold the address of any data type.
void *ptr;
*Pointer Operators (& and )
Passing arguments by pointers allows a function to modify the actual arguments of the
caller.
1. Function Definition:
2. Function Call:
int x = 5, y = 10;
swap(&x, &y); // Pass addresses of x and y
cout << x << " " << y; // Outputs: 10 5
3. Advantages:
a. Efficient memory use (avoids copying large data structures).
b. Enables functions to return multiple values indirectly.
Code Example
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swap(&x, &y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;
}
Output:
Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5
Conclusion
Pointers are a powerful feature in C++ that allow developers to write flexible and efficient
code. Mastering pointers is essential for understanding advanced programming concepts
like dynamic memory allocation, data structures, and efficient parameter passing.
Pointers and Arrays in C++
Pointers and arrays are closely related in C++. An array name itself acts as a constant
pointer to the first element of the array, making pointers essential when working with
arrays.
Key Concepts
1. Array as Pointer:
a. The name of an array (e.g., arr) represents the address of the first element in
the array.
b. Example: int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // 'ptr' now points to the first element of
'arr'
2. Pointer Arithmetic:
a. Pointers can traverse an array using arithmetic.
b. Example: *(ptr + 1) // Accesses the second element of the
array
Demonstration Code
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the array
// Access array elements using pointer
cout << "Accessing elements using pointers:" << endl;
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << *(ptr + i) << endl;
}
// Pointer arithmetic
cout << "\nPointer arithmetic demonstration:" << endl;
cout << "Address of first element: " << ptr << endl;
cout << "Address of second element: " << (ptr + 1) << endl;
cout << "Value at second element: " << *(ptr + 1) << endl;
return 0;
}
Explanation of Code
1. Pointer Initialization:
2. Accessing Elements:
a. *(ptr + i) accesses the i-th element of the array via pointer arithmetic.
b. arr[i] directly accesses the element using array indexing.
3. Modifying Elements:
a. *(ptr + i) += 5 modifies the array elements directly through the pointer.
4. Pointer Arithmetic:
a. Adding an integer to a pointer moves it to the next element of the array. The
step size depends on the data type (int in this case, typically 4 bytes).
Output
Conclusion
Pointers make array manipulation efficient, allowing traversal and modification without
explicit indexing. Understanding their interplay is crucial for tasks like dynamic memory
allocation and working with data structures in C++.