Passing pointer to function in c – Passing Pointer to Function in C Programming

Passing pointer to function in c: In C programming, we can pass the address of the variable to the formal arguments of a function. This method of calling a function by passing pointer arguments is known as call by reference. Any change in the value of formal parameters inside function will effect the value of actual argument. To pass a pointer to a function, we have to declare the function argument of pointer type.

C program to show how to pass pointer to a function

Passing Pointer to Function in C Programming

#include<stdio.h>
#include<conio.h>
   
void getDoubleValue(int *F){
   *F = *F + 2;
   printf("F(Formal Parameter) = %d\n", *F);
}
  
int main(){
   int A;
   printf("Enter a numbers\n");
   scanf("%d", &A);
   /* Calling function using call by reference */
   getDoubleValue(&A);
   /* Any change in the value of formal parameter(F)
   will effect the value of actual parameter(A) */
   printf("A(Actual Parameter) = %d\n", A);
     
   getch();
   return 0;
}
</conio.h></stdio.h>

Output

Enter a numbers
7
F(Formal Parameter) = 9
A(Actual Parameter) = 9

Passing an Array to a Function Using Pointers

Passing a pointer to a function in c: We can pass a one dimensional array to a function by passing the base address(address of first element of an array) of the array. We can either pas the name of the array(which is equivalent to base address) or pass the address of first element of array like &array[0]. Similarly, we can pass multi dimensional array also as formal parameters.
Different ways of declaring function which takes an array as input
Function argument as a pointer to the data type of array.

int testFunction(int *array){
  /* Function body */
} 

By specifying size of an array in function parameters.

int testFunction(int array[10]){
/* Function body */
}

By passing unsized array in function parameters.

int testFunction(int array[]){
/* Function body */
}

C Program to show different ways of passing pointer to a function

C Program to show different ways of passing pointer to a function

#include <stdio.h>
#include <conio.h>
  
/* This function takes integer pointer as input */
void printArraySumOne(int *array, int size){
    int i, sum = 0;
    for(i=0; i<size; i++){
     sum += array[i];
    }
    printf("1. Array sum : %d\n", sum);
}
  
/* This function takes sized array as input */
void printArraySumTwo(int array[5], int size){
    int i, sum = 0;
    for(i=0; i<size; i++){
     sum += array[i];
    }
    printf("2. Array sum : %d\n", sum);
}
  
/* This function takes unsized array as input */
void printArraySumThree(int array[], int size){
    int i, sum = 0;
    for(i=0; i<size; i++){
     sum += array[i];
    }
    printf("3. Array sum : %d\n", sum);
}
  
int main(){
   int array[5]={1, 2, 3, 4, 5};
    
   printArraySumOne(array, 5);
   printArraySumTwo(array, 5);
   printArraySumThree(array, 5);
     
   getch();
   return 0;
}

Output

1. Array sum : 15
2. Array sum : 15
3. Array sum : 15