• C Programming Video Tutorials

Passing pointers to functions in C



A pointer In C is a variable that stores the address of another variable. The pointer acts as a reference to the original variable. A pointer can be passed to a function, just like any other is passed. A function in C can be called in two ways −

  • Call by value and
  • Call by reference.

To call a function by reference, you need to define it to receive the pointer to a variable in the calling function.

Syntax

type function_name(type *var1, type *var2, . . .)

When a function is called by reference, the pointer of the actual argument variables passed, instead of their values.

Passing pointer to a function has two advantages −

  • It overcomes the limitation of pass by value. Changes to the value inside the called function are done directly at the address stored in the pointer. Hence, we can manipulate the variables in one scope from another.
  • It also overcomes the limitation of a function that it is able to return only one expression. By passing pointers, the effect of processing of a function takes place directly at the address. Secondly, imore than one values can be returned if we return pointer of an array or struct variable.

In this chapter, we shall see how to −

  • Pass pointers to int variables
  • pass pointers to array
  • pass pointer to structure

Let us define add() function that receives the references of two variables. When such a function is called, we pass the address of the actual argument. Let us call add() function by reference from inside main() function.

Example

#include <stdio.h>

/* function declaration */
int add(int *, int *);
int main(){
   int a=10, b=20;
   int c = add(&a, &b);
   printf("addition : %d", c);
}
int add(int *x, int *y){
   int z = *x + *y;
   
   return z;
}

Output

addition :30

One of the most cited applications of passing a pointer to a function is how we can swap the values of two variables.

Swap values by passing pointers

The following function receives the reference of two variables whose values are to be swapped.

/* function definition to swap the values */
int swap(int *x, int *y){
   int z;
   z = *x;    /* save the value at address x */
   *x = *y;      /* put y into x */
   *y = z;    /* put z into y */

   return 0;
}

The main() function has two variables a and b, their addresses are passes as arguments to swap() function.

Example

#include <stdio.h>
int swap(int *x, int *y){
   int z;
   z = *x;   
   *x = *y; 
   *y = z; 
}
int main () {

   /* local variable definition */
   int a = 10;
   int b = 20;
   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );
   
   /* calling a function to swap the values */
   swap(&a, &b);
   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );

   return 0;
}

Output

The result of the program when executed is −

Before swap, value of a : 10
Before swap, value of b : 20
After swap, value of a : 20
After swap, value of b : 10

Pass array pointer to a function

In C, the name of array is the address of the first element of the array, or in other words, the pointer to array. In the following example, we declare an uninitialized array in main() and pass its pointer to a function, along with an integer. Inside the function, the array is filled with the square, cube and square root. The function returns the pointer of this array, using which the values are access and printed in main() function.

Example

#include <stdio.h>
#include <math.h>
int  arrfunction(int, float *);
int main(){
   int x=100;
   float arr[3];
   arrfunction(x, arr);
   printf("Square of %d: %f\n", x, arr[0]);
   printf("cube of %d: %f\n", x, arr[1]);
   printf("Square root of %d: %f\n", x, arr[2]);
   
   return 0;
}
int arrfunction(int x, float *arr){
   arr[0]=pow(x,2);
   arr[1]=pow(x, 3);
   arr[2]=pow(x, 0.5);
}

Output

Square of 100: 10000.000000
cube of 100: 1000000.000000
Square root of 100: 10.000000

Pass string pointers to Function to compare lengths

Let us have a look at another example, where pointers are passed to a function. In the following program, two strings are passed to compare() functions. In C, as string is an array of char data type. We use strlen() function to find the length of string which is the number of characters in it.

Example

 
#include <stdio.h>
#include <string.h>
int compare( char *, char *);
int main() {
   char  a[] = "BAT";
   char  b[] = "BALL";
   int ret = compare(a, b);
   return 0;
}
int compare (char *x, char *y){
   int val;
   if (strlen(x)>strlen(y)){
      printf("length of string a is greater than or equal to length of string b");
   }
   else{
      printf("length of string a is less than length of string b");
   }
}

Output

length of string a is less than length of string b

Pass struct pointer to function

In C, a structure is a heterogenous data type, with its elements of different data types. In the example explained below, a struct variable of rectangle type is declared in main() and its address is passed to a user−defined function − area(). When called, the area() function is able to use the elements of the variable with the indirection operator −>. It computes the result and assigns it to the area element r−>area.

Example

#include <stdio.h>
#include <string.h>
struct rectangle{
   float len, brd;
   double area;
};
int area(struct rectangle *);
int main () {
   struct rectangle s;
   printf("Input length and breadth of a rectangle");
   scanf("%f %f", &s.len, &s.brd);
   area(&s);
   return 0;
}
int area(struct rectangle *r){
   r->area = (double)(r->len*r->brd);
   printf("Length: %f \n Breadth: %f \n Area: %lf\n", r->len, r->brd, r->area);
   return 0;
}

Output

Input length and breadth of a rectangle
10.5 20.5
Length: 10.500000 
Breadth: 20.500000 
Area: 215.250000

The logical extension of the concept of passing pointer to a function leads to passing Union pointer, pointer of a multi−dimensional array, passing pointer of a self−referential structure etc., all these have important uses in different application areas such as complex data structures, hardware control programming etc.

Advertisements