top of page

Pointer and functions

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Nov 8, 2024
  • 3 min read

Using pointers with functions in C and C++ allows for efficient data manipulation, as pointers enable functions to directly modify variables in memory or pass large data structures without copying them. This technique is fundamental for passing arrays, dynamically allocated memory, and complex data structures like structs. Let’s dive into the different ways pointers can be used with functions.


1. Passing Pointers to Functions

When you pass a pointer to a function, you allow that function to modify the value at the address the pointer refers to. This is also known as pass-by-reference.


Example: Swapping Two Numbers


In this example, we use a function to swap the values of two variables by passing pointers to those variables.


#include <stdio.h>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10, y = 20;
    printf("Before swap: x = %d, y = %d\n", x, y);
    
    swap(&x, &y);  // Pass the addresses of x and y
    
    printf("After swap: x = %d, y = %d\n", x, y);  // x and y are swapped
    return 0;
}

Explanation:

  • swap(int* a, int* b): a and b are pointers to int.

  • a and b dereference the pointers, allowing the function to modify x and y directly.

  • swap(&x, &y);: We pass the addresses of x and y to swap, allowing the function to modify the actual variables.


Output:

Before swap: x = 10, y = 20
After swap: x = 20, y = 10

  1. Passing Arrays to Functions

In C and C++, arrays are passed to functions as pointers to the first element. This allows the function to access and modify the original array elements without copying the entire array.

Example: Modifying an Array in a Function

#include <stdio.h>

void modifyArray(int* arr, int size) {
    for (int i = 0; i < size; i++) {
        arr[i] *= 2;  // Double each element
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);

    printf("Array before modification: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }

    modifyArray(arr, size);  // Passing the array as a pointer

    printf("\nArray after modification: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

Explanation:

  • void modifyArray(int* arr, int size): arr is a pointer to int, representing the address of the first element of the array.

  • The function doubles each element in the original array without copying the array.


Output:

Array before modification: 1 2 3 4 5
Array after modification: 2 4 6 8 10

  1. Using Pointer-to-Pointer (Double Pointers) in Functions

Double pointers are often used in functions to modify pointers themselves (e.g., dynamic memory allocation) or for handling arrays of pointers (e.g., 2D arrays).


Example: Allocating Memory with a Double Pointer


#include <stdio.h>
#include <stdlib.h>

void allocateMemory(int** ptr) {
    *ptr = (int*)malloc(sizeof(int) * 5);  // Allocates memory for an array of 5 integers
    if (*ptr == NULL) {
        printf("Memory allocation failed\n");
        return;
    }

    for (int i = 0; i < 5; i++) {
        (*ptr)[i] = i + 1;  // Initialize the array
    }
}

int main() {
    int* arr = NULL;

    allocateMemory(&arr);  // Pass the address of the pointer to allocate memory

    printf("Array elements after allocation and initialization: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }

    free(arr);  // Free allocated memory
    return 0;
}

Explanation:

  • void allocateMemory(int** ptr): ptr is a pointer to a pointer, allowing the function to allocate memory and assign it to arr in main.

  • ptr = (int)malloc(sizeof(int) * 5); allocates memory for an array of 5 integers and assigns it to the pointer in main.

  • Passing the address of arr (&arr) lets allocateMemory change arr directly.


Output:

Array elements after allocation and initialization: 1 2 3 4 5

Summary


  • Passing Pointers: Functions can modify the original data by receiving pointers to variables.

  • Arrays as Pointers: Arrays are passed as pointers to functions, which can modify the original array.

  • Double Pointers: Double pointers enable functions to modify pointer variables (e.g., dynamic memory allocation).


Pointers with functions enable more efficient memory usage, flexibility in modifying data, and are essential for dynamic programming and data structure manipulation.

 
 
 

Comments


bottom of page