C Programs Tutorials | IT Developer
IT Developer

C Programming - C Passing Pointers to Functions



Share with a Friend

C Programming - C Passing Pointers to Functions

C Passing Pointers to Functions

In C programming, passing a pointer to a function allows the function to modify the actual value of the variable passed. Instead of passing a copy of the variable (as with pass-by-value), passing a pointer allows the function to operate on the original variable, enabling direct modification of its value.

Passing pointers to functions is a common practice when dealing with large data structures (like arrays or structures) because it avoids unnecessary memory copying.

Syntax for Passing Pointers to Functions:

C

void function_name(type *ptr);

  • type: The data type the pointer points to.
  • *ptr: The pointer variable that points to the actual parameter.

Example: Passing Pointers to Functions

  1. Pass by Reference: Modify Variable Value in Function

In this example, a pointer is passed to a function to modify the value of the variable passed to the function.

C

#include <stdio.h>

// Function to modify the value of the passed integer using a pointer

void modifyValue(int *ptr) {

    *ptr = 20;  // Dereferencing the pointer to change the value it points to

}

int main() {

    int num = 10;

    printf("Before modifyValue function: num = %d\n", num);

    // Passing the address of 'num' to the function

    modifyValue(&num);

    printf("After modifyValue function: num = %d\n", num);  // num will be modified to 20

    return 0;

}

Output:

Before modifyValue function: num = 10

After modifyValue function: num = 20

In this example:

  • The function modifyValue accepts a pointer to an integer.
  • The main function passes the address of the variable num using the address-of operator (&).
  • Inside the function, the dereference operator (*) is used to modify the value at the memory address.
  1. Passing an Array to a Function Using Pointers

In C, arrays are passed to functions as pointers (i.e., the address of the first element of the array). Modifications to the array in the function will affect the original array.

C

#include <stdio.h>

// Function to modify an array

void modifyArray(int *arr, int size) {

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

        arr[i] = arr[i] * 2;  // Modify each element of the array

    }

}

int main() {

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

    int size = sizeof(arr) / sizeof(arr[0]);

    printf("Before modifyArray function:\n");

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

        printf("%d ", arr[i]);

    }

    printf("\n");

    // Passing the array (as pointer) to the function

    modifyArray(arr, size);

    printf("After modifyArray function:\n");

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

        printf("%d ", arr[i]);

    }

    printf("\n");

    return 0;

}

Output:

Before modifyArray function:

1 2 3 4 5

After modifyArray function:

2 4 6 8 10

In this example:

  • The array arr is passed to the modifyArray function, where it is treated as a pointer to the first element.
  • Each element of the array is doubled within the function.
  • The original array in the main function is modified because arrays are passed by reference (using pointers).
  1. Pass Pointer to a Structure

Pointers to structures can also be passed to functions. This allows the function to modify the structure's members directly.

C

#include <stdio.h>

// Define a structure

struct Point {

    int x;

    int y;

};

// Function to modify structure members

void modifyPoint(struct Point *p) {

    p->x = 50;

    p->y = 100;

}

int main() {

    struct Point pt = {10, 20};

    printf("Before modifyPoint function: x = %d, y = %d\n", pt.x, pt.y);

    // Passing pointer to structure

    modifyPoint(&pt);

    printf("After modifyPoint function: x = %d, y = %d\n", pt.x, pt.y);

    return 0;

}

Output:

Before modifyPoint function: x = 10, y = 20

After modifyPoint function: x = 50, y = 100

In this example:

  • We define a structure Point with members x and y.
  • We pass a pointer to the structure pt to the function modifyPoint.
  • Inside the function, we modify the structure members using the -> operator, which is used to access members of a structure through a pointer.

Advantages of Passing Pointers to Functions:

  1. Efficiency (Avoids Copying Large Data):
    • Passing a pointer to a function allows for the modification of large data structures (like arrays or structures) without copying them. This saves memory and processing time.
  2. Direct Modification:
    • It allows the function to directly modify the value of the passed variable, unlike pass-by-value, which only works with a copy of the data.
  3. Dynamic Memory Allocation:
    • Pointers enable functions to dynamically allocate memory (using malloc, calloc, or realloc) for structures or arrays.

Key Points to Remember:

  • When you pass a pointer to a function, you are passing the address of the variable, not a copy of it.
  • You can modify the original variable inside the function using dereferencing (*).
  • Pointers to arrays, structures, and other complex data types allow efficient and flexible management of data.
  • Always ensure that the pointer passed to the function is valid and points to allocated memory to avoid issues like segmentation faults or memory corruption.

Conclusion:

Passing pointers to functions is a powerful feature of C that provides efficient ways to manage memory and data. It enables direct modification of variables, arrays, and structures, making it essential for working with large data structures and performing operations that require altering the original data. Understanding pointers and their use in functions is fundamental to mastering C programming.