0% found this document useful (0 votes)
8 views5 pages

Pointers

Pointers in C++ are variables that store memory addresses of other variables, allowing for direct memory manipulation, efficient data structure handling, and dynamic memory allocation. They are declared with an asterisk (*) and can be used to pass arguments by reference, modify original values, and manage arrays. Proper pointer management is crucial to avoid issues like dangling pointers and memory leaks.

Uploaded by

Daud Khan
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)
8 views5 pages

Pointers

Pointers in C++ are variables that store memory addresses of other variables, allowing for direct memory manipulation, efficient data structure handling, and dynamic memory allocation. They are declared with an asterisk (*) and can be used to pass arguments by reference, modify original values, and manage arrays. Proper pointer management is crucial to avoid issues like dangling pointers and memory leaks.

Uploaded by

Daud Khan
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/ 5

Pointer in C++:

A pointer in C++ is a variable that stores the memory address of another variable. Instead of
holding a data value directly, it holds the location in memory where the data is stored.

Why Pointers?

 To manipulate memory directly.


 To pass large data structures efficiently to functions.
 For dynamic memory allocation.
 For building advanced data structures like linked lists, trees, graphs.

Pointer Syntax:

A pointer can be declared in the same way as any other variable but with an asterisk
symbol (*) as shown:

data_type* pointer_name;

 * → declares a pointer.
 & → retrieves the memory address (address-of operator).
 * (again) → dereferences a pointer (access the value it points to).

Example: Basic Pointer Usage

#include <iostream>

using namespace std;

int main() {

int num = 42;

int* ptr = &num; // ptr holds the address of num

cout << "Value of num: " << num << endl;

cout << "Address of num: " << &num << endl;

cout << "Pointer holds address: " << ptr << endl;

cout << "Value at pointer: " << *ptr << endl;

return 0;
}

Output:

Value of num: 42
Address of num: 0x61fefc
Pointer holds address: 0x61fefc
Value at pointer: 42

Explanation:

Term Description
int num = 42; Declares an integer variable num
int* ptr = &num; Declares a pointer that stores the address of num
*ptr Dereferences the pointer to get the value 42

Use Cases of Pointers

1. Function Arguments by Reference


Modifying the original value in the function.

void update(int* p)
{
*p = *p + 5;
}

2. Dynamic Memory Allocation

int* ptr = new int(10);

delete ptr; // Free the memory

3. Arrays and Strings

int arr[5] = {1, 2, 3, 4, 5};

int* p = arr; // array name is a pointer to the first element

Pointer Safety Tips


 Always initialize pointers (set to nullptr if not used).
 Avoid dangling pointers (pointing to freed memory).
 Always delete memory allocated using new.

Conclusion:

A pointer is a powerful feature in C++ used to access and manipulate memory directly. It
forms the basis for many advanced programming techniques and is essential for efficient system-
level development.

Passing Argument by Reference Using Pointers in C++:

In C++, you can pass a pointer to a function so that the function can modify the original
variable from the caller.

Example: Pass by Reference with Pointer

#include <iostream>

using namespace std;

// Function that modifies value through pointer

void updateValue(int* ptr) {

*ptr = *ptr + 10; // Modify the value at the pointer

int main() {

int num = 20;

cout << "Before function call: " << num << endl;

updateValue(&num); // Pass the address of num

cout << "After function call: " << num << endl;

return 0; }

Output:

Before function call: 20


After function call: 30
Explanation

Code Description

int* ptr Declares a pointer parameter

*ptr = *ptr + 10 Changes the original variable's value

updateValue(&num); Passes the address of num to function

Let’s compare both approaches: pass by pointer (reference) vs pass by value using the same
example:

Now: Pass by Value (Copy)

If you change the function like this:

void updateValue(int value) {

value = value + 10;

int main() {

int num = 20;

updateValue(num); // passing a copy

cout << num; // Output: ???

Output:

20

The original variable num remains unchanged because only a copy of num was passed.

Summary
Method Function Declaration Original Value Changed?

Pass by Value void updateValue(int n) ❌ No

Pass by Pointer (Reference) void updateValue(int* ptr) ✅ Yes

Conclusion:

When passing by value, the function works on a copy of the variable — changes are local and
not reflected outside the function.

When passing by pointer (or reference), the function works on the original variable —
changes are visible outside the function.

You might also like