C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Pointer Variables As Function Parameters in C Programming Language

In this tutorial, we will learn how to use pointer variables as function parameters in C programming language. Using pointer variables as function parameters enables you to pass the address of a variable to a function, allowing the function to modify the original value of the variable. This technique is known as "passing by reference."

Passing a pointer to a function:

To pass a pointer to a function, you should define the function with a pointer parameter of the appropriate data type. When calling the function, you pass the address of the variable using the address-of operator &.

Here's an example of a function that takes an integer pointer as a parameter and doubles the value of the integer it points to:

#include <stdio.h>

void double_value(int *num) {
    *num *= 2;
}

int main() {
    int x = 5;
    printf("Before doubling: %d\n", x);
    double_value(&x);
    printf("After doubling: %d\n", x);
    return 0;
}

Output:

Before doubling: 5
After doubling: 10

Swapping two variables using pointers:

One common use case for passing pointers to functions is to swap the values of two variables. Here's an example of a function that swaps the values of two integer variables:

#include <stdio.h>

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

int main() {
    int x = 5, y = 10;
    printf("Before swapping: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swapping: x = %d, y = %d\n", x, y);
    return 0;
}

Output:

Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5

Returning multiple values through pointer parameters:

By passing pointers as function parameters, you can return multiple values from a function. Here's an example of a function that takes two integers as input and returns their sum and product through pointer parameters:

#include <stdio.h>

void sum_and_product(int a, int b, int *sum, int *product) {
    *sum = a + b;
    *product = a * b;
}

int main() {
    int x = 5, y = 10, s, p;
    sum_and_product(x, y, &s, &p);
    printf("Sum: %d, Product: %d\n", s, p);
    return 0;
}

Output:

Sum: 15, Product: 50

In summary, using pointer variables as function parameters in C programming language enables you to pass variables by reference. This allows functions to modify the original values of variables and return multiple values through pointer parameters. Understanding how to pass pointers to functions is essential for creating flexible and efficient C programs.

  1. Passing Pointers to Functions in C Language:

    Pointers can be passed to functions to allow the function to access and modify data outside its scope.

    #include <stdio.h>
    
    void increment(int *num) {
        (*num)++;
    }
    
    int main() {
        int number = 5;
        increment(&number); // Passing a pointer to the function
        printf("Incremented Value: %d\n", number);
        return 0;
    }
    

    Output:

    Incremented Value: 6
    

    increment function takes a pointer to an integer and increments the value.

  2. Benefits of Using Pointer Variables in Function Parameters:

    Using pointers as function parameters allows modifying the original data directly, saving memory and enhancing performance.

    #include <stdio.h>
    
    void swap(int *a, int *b) {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
    
    int main() {
        int x = 5, y = 10;
        swap(&x, &y); // Swapping values using pointers
        printf("Swapped Values: x=%d, y=%d\n", x, y);
        return 0;
    }
    

    Output:

    Swapped Values: x=10, y=5
    

    Using pointers as parameters allows functions to directly modify the original data.

  3. Modifying Values Through Pointer Parameters in C Programming:

    Functions can modify the original values by dereferencing pointers.

    #include <stdio.h>
    
    void square(int *num) {
        *num = (*num) * (*num);
    }
    
    int main() {
        int value = 4;
        square(&value); // Squaring the value using a pointer
        printf("Squared Value: %d\n", value);
        return 0;
    }
    

    Output:

    Squared Value: 16
    

    The square function modifies the original value using a pointer parameter.

  4. C Code Examples Demonstrating Pointer Parameters in Functions:

    #include <stdio.h>
    
    void modifyArray(int *arr, int size) {
        for (int i = 0; i < size; ++i) {
            arr[i] *= 2;
        }
    }
    
    int main() {
        int numbers[] = {1, 2, 3, 4, 5};
        int size = sizeof(numbers) / sizeof(numbers[0]);
    
        modifyArray(numbers, size); // Modifying array elements using pointers
    
        for (int i = 0; i < size; ++i) {
            printf("%d ", numbers[i]);
        }
    
        return 0;
    }
    

    Output:

    2 4 6 8 10
    

    The modifyArray function modifies array elements using a pointer.

  5. Pointer Parameters vs. Passing by Value in C:

    Passing pointers allows modifying the original data, unlike passing by value.

    #include <stdio.h>
    
    void incrementByValue(int num) {
        num++;
    }
    
    void incrementByPointer(int *num) {
        (*num)++;
    }
    
    int main() {
        int x = 5, y = 5;
    
        incrementByValue(x); // Does not modify x
        incrementByPointer(&y); // Modifies y using a pointer
    
        printf("Value of x: %d\n", x);
        printf("Value of y: %d\n", y);
    
        return 0;
    }
    

    Output:

    Value of x: 5
    Value of y: 6
    

    incrementByValue doesn't modify x, while incrementByPointer modifies y.

  6. Avoiding Common Pitfalls with Pointer Parameters in C Functions:

    Ensure that pointers are valid and non-null before dereferencing.

    #include <stdio.h>
    
    void safeIncrement(int *num) {
        if (num != NULL) {
            (*num)++;
        }
    }
    
    int main() {
        int value = 5;
        int *ptr = NULL;
    
        safeIncrement(&value); // Safely incrementing using a pointer
        safeIncrement(ptr); // Avoiding dereferencing a null pointer
    
        printf("Incremented Value: %d\n", value);
    
        return 0;
    }
    

    Output:

    Incremented Value: 6
    

    Using a conditional check to avoid dereferencing a null pointer.

  7. Function Prototypes with Pointer Parameters in C:

    Declare function prototypes with pointer parameters.

    #include <stdio.h>
    
    // Function prototype with a pointer parameter
    void doubleValue(int *num);
    
    int main() {
        int number = 3;
        doubleValue(&number); // Calling the function with a pointer parameter
        printf("Doubled Value: %d\n", number);
        return 0;
    }
    
    // Function definition with a pointer parameter
    void doubleValue(int *num) {
        *num *= 2;
    }
    

    Output:

    Doubled Value: 6
    

    Declare the function prototype before using the function in the main code.

  8. Pointers to Arrays as Function Parameters in C:

    Pointers can be used to pass arrays to functions efficiently.

    #include <stdio.h>
    
    void printArray(int *arr, int size) {
        for (int i = 0; i < size; ++i) {
            printf("%d ", arr[i]);
        }
    }
    
    int main() {
        int numbers[] = {1, 2, 3, 4, 5};
        int size = sizeof(numbers) / sizeof(numbers[0]);
    
        printArray(numbers, size); // Passing array using a pointer
    
        return 0;
    }
    

    Output:

    1 2 3 4 5
    

    Passing an array to a function using a pointer allows efficient array handling.