- C Programming Tutorial
- C - Home
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Booleans
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- C - Storage Classes
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- C - Functions
- C - Main Functions
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Passing Pointers to Functions
- C - Strings
- C - Array of Strings
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Pointers to Structures
- C - Self-Referential Structures
- C - Nested Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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.
To Continue Learning Please Login
Login with Google