C++ Program for Pointers

01/06/2024 0 By indiafreenotes

Pointers are variables that store memory addresses. They are essential for dynamic memory management, array handling, and implementing complex data structures. Here are the basics:

  1. Pointer Declaration and Initialization

int *ptr; // Declaration of a pointer

int var = 10;

ptr = &var; // Initialization with the address of var

  1. Dereferencing Pointers

int value = *ptr; // Dereference ptr to get the value of var

  1. Pointer Arithmetic

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

int *ptr = arr; // Points to the first element of arr

ptr++; // Now points to the second element of arr

  1. Dynamic Memory Allocation

int *ptr = new int; // Allocate memory for an integer

*ptr = 10; // Assign a value to the allocated memory

delete ptr; // Deallocate the memory

  1. Function Pointers

void func(int a) {

    cout << a << endl;

}

void (*func_ptr)(int) = &func; // Function pointer

func_ptr(5); // Calling the function using the pointer

Example Program

Here’s a program that demonstrates several key aspects of pointers in C++:

#include <iostream>

using namespace std;

// Function to swap two integers using pointers

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

    int temp = *a;

    *a = *b;

    *b = temp;

}

 

// Function to dynamically allocate an array and fill it with values

int* createArray(int size) {

    int *arr = new int[size];

    for (int i = 0; i < size; ++i) {

        arr[i] = i + 1;

    }

    return arr;

}

 

// Function to print the elements of an array

void printArray(int *arr, int size) {

    for (int i = 0; i < size; ++i) {

        cout << arr[i] << ” “;

    }

    cout << endl;

}

 

// Function that takes a function pointer as an argument

void applyFunction(void (*func)(int), int value) {

    func(value);

}

 

// Simple function to demonstrate function pointers

void display(int a) {

    cout << “Value is: ” << a << endl;

}

 

int main() {

    // Basic pointer usage

    int var = 42;

    int *ptr = &var;

    cout << “Address of var: ” << ptr << endl;

    cout << “Value of var: ” << *ptr << endl;

 

    // Swapping values using pointers

    int x = 10, y = 20;

    cout << “Before swap: x = ” << x << “, y = ” << y << endl;

    swap(&x, &y);

    cout << “After swap: x = ” << x << “, y = ” << y << endl;

 

    // Dynamic memory allocation

    int size;

    cout << “Enter the size of the array: “;

    cin >> size;

    int *array = createArray(size);

    cout << “Array elements: “;

    printArray(array, size);

    delete[] array;

 

    // Function pointers

    void (*func_ptr)(int) = display;

    applyFunction(func_ptr, 5);

 

    return 0;

}

Explanation:             

  • Basic Pointer Usage

Declares an integer and a pointer to that integer. Prints the address and value using the pointer.

  • Swapping Values Using Pointers

The swap function demonstrates passing pointers to a function to swap the values of two variables.

  • Dynamic Memory Allocation

Prompts the user for the size of an array, dynamically allocates memory for the array, fills it with values, prints the array, and then deallocates the memory.

  • Function Pointers

Defines a simple function display and a function applyFunction that takes a function pointer as an argument. Demonstrates calling the display function using a function pointer.