0% found this document useful (0 votes)
17 views7 pages

POINTERS CONSTRUCT

pointer notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

POINTERS CONSTRUCT

pointer notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

THE POINTERS CONSTRUCT:

Concept of pointers.

Pointers overview.

Pointer declaration and control.

Pointer operator (& and *).

Parameter passing by pointers. in c++

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

• Definition: A pointer is a variable that holds the memory address of another


variable.
• Usage: Pointers are used for:
o Dynamic memory allocation
o Passing arguments by reference
o Accessing array elements efficiently
o Implementing data structures like linked lists, trees, etc.

Pointers Overview

1. Pointer Variable: A pointer is declared using the * operator.

int *ptr; // A pointer to an integer

2. Memory Address: Pointers store the address of variables.


int x = 10;
int *ptr = &x; // 'ptr' stores the address of 'x'

3. Dereferencing: Accessing the value stored at the memory location pointed to by a


pointer.

cout << *ptr; // Prints the value of 'x'

Pointer Declaration and Control

• Declaration Syntax:

dataType *pointerName;

Example:

int *p; // Pointer to an integer


double *d; // Pointer to a double

• Initialization: A pointer should be initialized before use.

int x = 5;
int *p = &x; // Initialize pointer to address of 'x'

• Null Pointers: Pointers can be initialized to nullptr to indicate they point to


nothing.

int *ptr = nullptr;

• Void Pointers: A pointer that can hold the address of any data type.

void *ptr;
*Pointer Operators (& and )

1. Address-of Operator (&):


a. Used to get the memory address of a variable.
int x = 10;
int *ptr = &x; // 'ptr' now holds the address of 'x'

2. Dereference Operator (*):


a. Used to access the value at the address a pointer holds.
int x = 10;
int *ptr = &x;
cout << *ptr; // Outputs 10

Parameter Passing by Pointers

Passing arguments by pointers allows a function to modify the actual arguments of the
caller.

1. Function Definition:

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

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;

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

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

3. Accessing Array Elements via Pointer:


a. Array elements can be accessed using either array indexing or pointer
dereferencing.
b. Example: cout << arr[2]; // Using array indexing
cout << *(ptr + 2); // Using pointer dereferencing

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;
}

// Modifying array elements via pointer


cout << "\nModifying elements using pointers:" << endl;
for (int i = 0; i < 5; i++) {
*(ptr + i) += 5; // Increment each element by 5
cout << "Modified Element " << i << ": " << arr[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:

int *ptr = arr;

Here, ptr points to the first element of the array.

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

Accessing elements using pointers:


Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

Modifying elements using pointers:


Modified Element 0: 15
Modified Element 1: 25
Modified Element 2: 35
Modified Element 3: 45
Modified Element 4: 55

Pointer arithmetic demonstration:


Address of first element: 0x7ffee48c1910
Address of second element: 0x7ffee48c1914
Value at second element: 25

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++.

You might also like