- C Programming Tutorial
- C - Home
- Basics of C
- 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 - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- 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
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- 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
- Functions in C
- C - Functions
- C - Main Function
- 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
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- 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. It acts as a reference to the original variable. A pointer can be passed to a function, just like any other argument is passed.
A function in C can be called in two ways −
To call a function by reference, you need to define it to receive the pointer to a variable in the calling function. Here is the syntax that you would use to call a function by reference −
type function_name(type *var1, type *var2, ...)
When a function is called by reference, the pointers of the actual argument variables are passed, instead of their values.
Advantages of Passing Pointers to Functions
Passing a 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 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.
In this chapter, we shall see how to −
- Pass pointers to int variables
- Pass pointers to array
- Pass pointer to structure
Example of Passing Pointers to Functions
Let us define a function add() that receives the references of two variables. When such a function is called, we pass the address of the actual argument. Let us call the add() function by reference from inside the main() function.
#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
Swap Values by Passing Pointers
One of the most cited applications of passing a pointer to a function is how we can swap the values of two variables.
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; }
Example
The main() function has two variables a and b; their addresses are passed as arguments to the swap() function.
#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
When you execute this code, it will produce the following output −
Before swap, value of a: 10 Before swap, value of b: 20 After swap, value of a: 20 After swap, value of b: 10
Passing an Array Pointer to a Function
In C programming, the name of an array acts the address of the first element of the array; in other words, it becomes a pointer to the array.
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 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
When you run this code, it will produce the following output −
Square of 100: 10000.000000 cube of 100: 1000000.000000 Square root of 100: 10.000000
Passing String Pointers to a Function
Let us have a look at another example, where we will pass string pointers to a function.
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 compare(char *, char *); int main(){ char str1[] = "BAT"; char str2[] = "BALL"; int ret = compare(str1, str2); return 0; } int compare (char *x, char *y){ int val; if (strlen(x) > strlen(y)){ printf("Length of Str1 is greater than or equal to the length of Str2"); } else{ printf("Length of Str1 is less than the length of Str2"); } }
Output
When you run this code, it will produce the following output −
Length of Str1 is less than the length of Str2
Passing Struct Pointer to a Function
In C programming, a structure is a heterogenous data type containing elements of different data types. Let's see how we can pass a struct pointer to a function.
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 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
Run the code and check its 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 a pointer to a function leads to passing a Union pointer, i.e., the pointer of a multi-dimensional array, passing the 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.