C++ Pointers


Pointers are one of the most powerful features in C++. They allow you to directly work with memory by storing the address of variables. Once you understand how pointers work, you can manage memory efficiently, pass data to functions more effectively, and build complex structures like linked lists, trees, and dynamic arrays.

What is a Pointer in C++?

A pointer is a variable that stores the memory address of another variable instead of holding a value itself.
Every variable in a program has a unique address in memory, and a pointer helps access or modify the data stored at that address.

Pointer Declaration Syntax

dataType *pointerName;

Example:

int number = 25;
int *ptr = &number;

Here:

  • number is a normal integer variable.

  • ptr is a pointer that holds the address of number.

  • & is the address-of operator.

  • * is the dereference operator.

Declaring and Initializing Pointers

Pointers can be declared for different data types:

int *p1;
char *p2;
float *p3;
double *p4;

After declaration, you initialize them with the address of a variable:

int x = 10;
int *ptr = &x;

Now, ptr stores the address of x.

Accessing Values Using Pointers

The dereference operator (*) allows you to access or modify the value stored at the memory address a pointer holds.

Example:

int num = 5;
int *p = #

cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Value stored in pointer p: " << p << endl;
cout << "Value pointed by p: " << *p << endl;

Output:

Value of num: 5
Address of num: 0x61ff08
Value stored in pointer p: 0x61ff08
Value pointed by p: 5

Modifying Values Using Pointers

You can change the original variable’s value through its pointer because both refer to the same memory address.

int a = 20;
int *p = &a;

*p = 50; // modifies the value of 'a'
cout << "New value of a: " << a << endl;

Output:

New value of a: 50

Null Pointers in C++

A null pointer does not point to any valid memory location. It’s a good practice to initialize pointers as null before assigning them an address.

int *ptr = nullptr;

if (ptr == nullptr) {
    cout << "Pointer is null.";
}

This prevents undefined behavior caused by accessing invalid memory.

Pointer Arithmetic

Pointers support basic arithmetic operations like increment (++) and decrement (--). This feature is often used to work with arrays.

Example:

int arr[3] = {10, 20, 30};
int *p = arr;

cout << *p << endl;    // 10
p++;
cout << *p << endl;    // 20
p++;
cout << *p << endl;    // 30

Each time you increment the pointer, it moves to the next element’s address in memory.

Pointers and Arrays

In C++, an array name acts like a constant pointer to its first element.
This means you can use pointers to access or manipulate array values easily.

Example:

int marks[3] = {85, 90, 95};
int *ptr = marks;

for (int i = 0; i < 3; i++) {
    cout << *(ptr + i) << " ";
}

Output:

85 90 95

Pointers and Functions

Pointers are commonly used to pass variables by reference to functions, allowing the function to modify the original data.

Example:

void updateValue(int *p) {
    *p = *p + 10;
}

int main() {
    int num = 5;
    updateValue(&num);
    cout << "Updated number: " << num;
}

Output:

Updated number: 15

Here, the function receives the address of num and updates its value directly.

Pointer to Pointer

A pointer can also store the address of another pointer. This is known as a pointer to pointer or a double pointer.

Example:

int value = 100;
int *ptr = &value;
int **pptr = &ptr;

cout << "Value: " << **pptr;

Output:

Value: 100

This feature is used in advanced programming when managing multiple levels of data references.

Void Pointers in C++

A void pointer is a generic pointer type that can hold the address of any data type.
However, you must use typecasting before dereferencing it.

Example:

int a = 10;
void *ptr = &a;
cout << *(int*)ptr;

Dangling Pointers

A dangling pointer occurs when a pointer refers to a memory location that has been deleted or is no longer valid.

Example:

int *ptr = new int(10);
delete ptr;  // memory freed
ptr = nullptr; // avoid dangling pointer

Always set pointers to nullptr after freeing memory to prevent undefined behavior.

Dynamic Memory and Pointers

Dynamic memory allocation allows you to create variables or arrays during runtime using the new operator and release them using delete.

Example:

int *p = new int;
*p = 25;
cout << *p << endl;
delete p;
p = nullptr;

Pointers make it possible to efficiently manage memory and build flexible programs.

Summary of C++ Pointers

  • A pointer stores the address of another variable.

  • The * operator dereferences, while & gets an address.

  • Pointers can traverse arrays and pass data to functions.

  • Use nullptr to avoid accessing invalid memory.

  • Pointers are essential for dynamic memory management.


Practice Questions

  1. Write a C++ program to display the address and value of a variable using a pointer.

  2. Create a program to swap two numbers using pointers (without using a third variable).

  3. Write a function that accepts the address of an integer and increments its value by 10.

  4. Build a program to find the sum of all elements in an array using pointers.

  5. Write a C++ program that reverses an array using pointer arithmetic.

  6. Create a program to demonstrate pointer-to-pointer usage with integer variables.

  7. Implement a function that dynamically allocates memory for an integer array and displays its elements.

  8. Write a program that uses pointers to copy one string into another without using standard library functions.

  9. Create a program to count even and odd numbers in an array using pointer traversal.

  10. Write a C++ program to find the maximum and minimum values in an array using pointers.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top