Pass pointer to function in C programming

In C programming, we can pass pointer to function. In other words, we can pass the address of a variable to a function. We have to pass the address and for this we can pass pointer as the argument of a function.

In this C tutorial guide we will learn about how we can pass a pointer to any function using C language.

Let’s see the syntax of passing pointer to a function.

// syntax of passing pointer to function in c

void myFunct(int* num1, int* num2){
  // code to execute here

int main(){
    int a = 5, b = 10;
    myFunct( &a, &b);  // pass address to the function

Let’s take an example of C program to learn how to pass pointer to the function.

Swap number program (pass pointer to function)

In this program we will pass two address of integer variable to a function then we will use their value to swap them. Let’s consider the example bellow;

// c program to pass pointer to function

#include <stdio.h>
void swap_num(int* a, int* b){
  int t;
  t = *a;
  *a = *b;
  *b = t;

int main(){
  int x, y;
  printf("Enter the value of x and y : ");
  scanf("%d %d", &x, &y);
  printf("\nYou have entered ........\n");
  printf("x = %d\ny = %d\n", x, y);

  swap_num(&x, &y);

  printf("\nAfter swapping ........\n");
  printf("x = %d\ny = %d\n", x, y);
  return 0;

Output of this pointer to function program


Previous pagePointer to pointer in C

Next pageDynamic memory allocation

Recommended for you: