Passing Pointers To Functions in C
Passing Pointers To Functions in C
Passing Pointers To Functions in C
Call by Value
Call by Reference
When a function is called by reference, the pointers of the actual argument variables
are passed, instead of their values.
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 can return only one
expression. By passing pointers, the effect of processing of a function takes
place directly at the address. Secondly, more than one values can be returned
if we return a pointer of an array or struct variable.
https://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm 1/7
6/16/24, 1:04 PM Passing Pointers to Functions in C
#include <stdio.h>
/* function declaration */
int add(int *, int *);
int main(){
return z;
}
Output
Addition: 30
The following function receives the reference of two variables whose values are to be
swapped −
https://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm 2/7
6/16/24, 1:04 PM Passing Pointers to Functions in C
return 0;
}
Example
The main() function has two variables a and b; their addresses are passed as
arguments to the swap() function.
#include <stdio.h>
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm 3/7
6/16/24, 1:04 PM Passing Pointers to Functions in C
When you execute this code, it will produce the following output −
Example
In this 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 of the
passed integer. The function returns the pointer of this array, using which the values
are accessed and printed in the main() function.
#include <stdio.h>
#include <math.h>
int main(){
int x = 100;
float arr[3];
arrfunction(x, arr);
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm 4/7
6/16/24, 1:04 PM Passing Pointers to Functions in C
Output
When you run this code, it will produce the following output −
Example
In this program, two strings are passed to the compare() function. A string in C is an
array of char data type. We use the strlen() function to find the length of the string.
#include <stdio.h>
int main(){
return 0;
}
int val;
https://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm 5/7
6/16/24, 1:04 PM Passing Pointers to Functions in C
}
else{
printf("Length of Str1 is less than the length of Str2");
}
}
Output
When you run this code, it will produce the following output −
Example
In this example, a struct variable rectangle is declared in main() and its address is
passed to a user-defined function called 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.
#include <stdio.h>
#include <string.h>
struct rectangle{
float len, brd;
double area;
};
int main(){
struct rectangle s;
printf("Input length and breadth of a rectangle");
scanf("%f %f", &s.len, &s.brd);
area(&s);
https://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm 6/7
6/16/24, 1:04 PM Passing Pointers to Functions in C
return 0;
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
https://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm 7/7