Simple Example Program for Passing pointers to functions In C


Pass By Reference Definition

  • Each parameter was followed by an ampersand sign (&). (&) will pass an address of the original variable instead of by value.
  • In Passed by reference, the original variable and the function variable use the same memory/variable for the parameter. If the function variable modifies the parameter value, the effect is visible to the original variable.

Syntax

//Function - Pass By Reference
void fn_name(int *var1,int *var2)
{
   // Code
}

//Main Code- Pass By Reference
fn_name(&orginal_var1,&orginal_var2);

Simple Example Program for Passing pointers to functions

/*##Simple Example Program for Passing pointers to functions In C*/
/*##Simple Add Numbers Programs,pointers Example C Programming*/
/*##Pass By Referense Function Example Program In C Programming*/

#include <stdio.h>

// Declare Add Numbers Function Using Pointer 

void add_numbers(int *value1, int *value2, int *result) {
  *result = *value1 + *value2;
}

int main() {
  // Declare Variables
  int number1, number2, result = 0;

  // Read User Input
  printf("Enter value of Number # 1: ");
  scanf("%d", &number1);
  printf("Enter value of Number # 2: ");
  scanf("%d", &number2);

  //Print Values Pass to Reference
  printf("Before Pass to Reference :  Number # 1=%d,  Number # 2=%d, Result # :%d \n", number1, number2, result);

  //Call add_numbers Function By Passing Reference
  add_numbers(&number1, &number2, &result);

  //Print Values Pass to Reference
  printf("After  Pass to Reference :  Number # 1=%d,  Number # 2=%d, Result # :%d\n", number1, number2, result);

  return 0;
}

Sample Output

Enter value of Number # 1: 5
Enter value of Number # 2: 6
Before Pass to Reference :  Number # 1=5,  Number # 2=6, Result # :0
After  Pass to Reference :  Number # 1=5,  Number # 2=6, Result # :11