- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Passing Pointers to Functions
![]() Share with a Friend |
C Programming - C Passing Pointers to Functions
C Passing Pointers to Functions
In C programming, passing a pointer to a function allows the function to modify the actual value of the variable passed. Instead of passing a copy of the variable (as with pass-by-value), passing a pointer allows the function to operate on the original variable, enabling direct modification of its value.
Passing pointers to functions is a common practice when dealing with large data structures (like arrays or structures) because it avoids unnecessary memory copying.
Syntax for Passing Pointers to Functions:
C
void function_name(type *ptr);
- type: The data type the pointer points to.
- *ptr: The pointer variable that points to the actual parameter.
Example: Passing Pointers to Functions
- Pass by Reference: Modify Variable Value in Function
In this example, a pointer is passed to a function to modify the value of the variable passed to the function.
C
#include <stdio.h>
// Function to modify the value of the passed integer using a pointer
void modifyValue(int *ptr) {
*ptr = 20; // Dereferencing the pointer to change the value it points to
}
int main() {
int num = 10;
printf("Before modifyValue function: num = %d\n", num);
// Passing the address of 'num' to the function
modifyValue(&num);
printf("After modifyValue function: num = %d\n", num); // num will be modified to 20
return 0;
}
Output:
Before modifyValue function: num = 10
After modifyValue function: num = 20
In this example:
- The function modifyValue accepts a pointer to an integer.
- The main function passes the address of the variable num using the address-of operator (&).
- Inside the function, the dereference operator (*) is used to modify the value at the memory address.
- Passing an Array to a Function Using Pointers
In C, arrays are passed to functions as pointers (i.e., the address of the first element of the array). Modifications to the array in the function will affect the original array.
C
#include <stdio.h>
// Function to modify an array
void modifyArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2; // Modify each element of the array
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Before modifyArray function:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Passing the array (as pointer) to the function
modifyArray(arr, size);
printf("After modifyArray function:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Before modifyArray function:
1 2 3 4 5
After modifyArray function:
2 4 6 8 10
In this example:
- The array arr is passed to the modifyArray function, where it is treated as a pointer to the first element.
- Each element of the array is doubled within the function.
- The original array in the main function is modified because arrays are passed by reference (using pointers).
- Pass Pointer to a Structure
Pointers to structures can also be passed to functions. This allows the function to modify the structure's members directly.
C
#include <stdio.h>
// Define a structure
struct Point {
int x;
int y;
};
// Function to modify structure members
void modifyPoint(struct Point *p) {
p->x = 50;
p->y = 100;
}
int main() {
struct Point pt = {10, 20};
printf("Before modifyPoint function: x = %d, y = %d\n", pt.x, pt.y);
// Passing pointer to structure
modifyPoint(&pt);
printf("After modifyPoint function: x = %d, y = %d\n", pt.x, pt.y);
return 0;
}
Output:
Before modifyPoint function: x = 10, y = 20
After modifyPoint function: x = 50, y = 100
In this example:
- We define a structure Point with members x and y.
- We pass a pointer to the structure pt to the function modifyPoint.
- Inside the function, we modify the structure members using the -> operator, which is used to access members of a structure through a pointer.
Advantages of Passing Pointers to Functions:
- Efficiency (Avoids Copying Large Data):
- Passing a pointer to a function allows for the modification of large data structures (like arrays or structures) without copying them. This saves memory and processing time.
- Direct Modification:
- It allows the function to directly modify the value of the passed variable, unlike pass-by-value, which only works with a copy of the data.
- Dynamic Memory Allocation:
- Pointers enable functions to dynamically allocate memory (using malloc, calloc, or realloc) for structures or arrays.
Key Points to Remember:
- When you pass a pointer to a function, you are passing the address of the variable, not a copy of it.
- You can modify the original variable inside the function using dereferencing (*).
- Pointers to arrays, structures, and other complex data types allow efficient and flexible management of data.
- Always ensure that the pointer passed to the function is valid and points to allocated memory to avoid issues like segmentation faults or memory corruption.
Conclusion:
Passing pointers to functions is a powerful feature of C that provides efficient ways to manage memory and data. It enables direct modification of variables, arrays, and structures, making it essential for working with large data structures and performing operations that require altering the original data. Understanding pointers and their use in functions is fundamental to mastering C programming.
