C Programming Tutorial

C Programming Tutorial

Passing Pointers to Function in C

Hello Everyone, In this article, we will cover the concept of Passing Pointers to Function in C Programming. We will cover how pointers work, how to pass them to functions, and provide examples to help you grasp this important programming technique. Whether you’re new to C or have some experience, this guide will help you to understand concet of Passing Pointers to Function in C effectively. Let’s get started.

In C programming, pointers can be passed to functions as arguments. This is particularly useful when you want a function to modify the value of a variable or to work with large data structures like arrays without making a copy of the data. When a pointer is passed to a function, the function can access and modify the memory location that the pointer points to.

Example of passing pointers to a function in C programming

#include <stdio.h>
void modifyValue(int *ptr) {
    *ptr = *ptr * 2;
}

int main() {
    int num = 5;
    printf("Before modifying: %d\\n", num);

    // Pass the address of the variable 'num' to the function
    modifyValue(&num);

    printf("After modifying: %d\\n", num);

    return 0;
}

Explanation:

  1. We first include the standard I/O library using #include <stdio.h>.
  2. We declare a function called modifyValue that takes a pointer to an integer as an argument.
  3. Inside the modifyValue function, we modify the value at the memory address pointed to by the pointer ptr. In this example, we multiply the value by 2.
  4. In the main function, we declare an integer variable num and initialize it to 5.
  5. We print the value of num before calling the modifyValue function.
  6. We call the modifyValue function, passing the address of the num variable using the & operator. This allows the function to access and modify the original num variable.
  7. We print the value of num after calling the modifyValue function.

Output:

Before modifying: 5
After modifying: 10

In this example, the value of num is modified from 5 to 10 by passing a pointer to the modifyValue function.

Example of passing pointers to a function in C programming

#include <stdio.h>
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10;
    int y = 20;
    printf("Before swapping: x = %d, y = %d\\n", x, y);

    // Pass the addresses of the variables 'x' and 'y' to the function
    swap(&x, &y);

    printf("After swapping: x = %d, y = %d\\n", x, y);

    return 0;
}

Explanation:

  1. We include the standard I/O library using #include <stdio.h>.
  2. We declare a function called swap that takes two pointers to integers as arguments.
  3. Inside the swap function, we use a temporary variable temp to store the value of the memory location pointed to by the pointer a. Then, we assign the value of the memory location pointed to by the pointer b to the memory location pointed to by the pointer a. Finally, we assign the value of temp to the memory location pointed to by the pointer b. This effectively swaps the values of the two memory locations.
  4. In the main function, we declare two integer variables x and y, and initialize them to 10 and 20, respectively.
  5. We print the values of x and y before calling the swap function.
  6. We call the swap function, passing the addresses of the x and y variables using the & operator. This allows the function to access and modify the original x and y variables.
  7. We print the values of x and y after calling the swap function.

Output:

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

In this example, the values of x and y are swapped by passing pointers to the swap function.

Share This Article