For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
Now Reading
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
In C, a function pointer is a pointer that points to the address of a function in memory rather than pointing to a data value. Function pointers in C allow functions to be passed as arguments to other functions, returned as values from functions, and even stored in data structures.
This adds a great deal of flexibility to your C programs, enabling dynamic function calls, callbacks, and other advanced programming techniques.
Function pointers allow you to:
To understand how function pointers work, consider that in C, functions are also stored in memory-like variables. When you declare a function, the compiler stores its address, allowing you to refer to it using a function pointer.
Here’s a simple breakdown:
Improve your C programming skills with our Software Development courses — take the next step in your learning journey!
Now, Let’s understand the syntax of the function pointers in C. The syntax for declaring a function pointer is:
return_type (*pointer_name)(parameter_types);
Where:
The parentheses around (*pointer_name) are crucial. If you omit them, the compiler will assume you are declaring a function, not a pointer to a function.
Let’s break this down further with an example.
Let’s declare a function pointer for a function that takes two int parameters and returns an int.
int (*add_ptr)(int, int);
Here,
So, add_ptr is a pointer to a function that takes two ints as input and returns an int.
Also Read: Command Line Arguments in C Explained
To assign a function to a pointer, simply use the name of the function (without parentheses) to point to the function’s memory address.
Here’s how you do it:
add_ptr = &add; // Here, add is a function name, & is optional
Here,
To call the function via the pointer, you dereference the pointer and call it just like you would call the function directly.
For example:
int result = add_ptr(3, 4); // This calls add(3, 4) via the function pointer
Alternatively, you can dereference the pointer explicitly:
int result = (*add_ptr)(3, 4); // Equivalent to the previous call
Both methods will execute the function ‘add’ with arguments 3 and 4.
In C programming, referencing, and dereferencing are operations that are fundamental to how pointers work. They are especially important when working with function pointers.
Regarding function pointers, referencing and dereferencing allow you to point to a function and then call that function dynamically. The syntax of these operations is essential to understand in order to use function pointers effectively.
Referencing a function pointer means assigning the function's address to a function pointer variable. This operation essentially binds the pointer to a function, which can then be invoked indirectly through that pointer.
In the case of function pointers, referencing is done by simply assigning the function name (without parentheses) to the pointer.
Here’s a basic example to demonstrate how referencing works with function pointers.
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
// Declare a function pointer and reference it to the add function
int (*add_ptr)(int, int) = add;
// Calling the function through the function pointer
int result = add_ptr(3, 4);
printf("Addition result: %d\n", result); // Output: 7
return 0;
}
Explanation:
Output:
Addition result: 7
In this case, referencing allows us to store the address of the add function in the pointer add_ptr and call it indirectly.
Dereferencing a function pointer means accessing the function that the pointer is pointing to and calling it. When you dereference a function pointer, you're telling the program to go to the memory address stored in the pointer and execute the function stored there.
In C, dereferencing a function pointer is done by calling the pointer as if it were the function itself. This can be done either implicitly or explicitly:
Let’s look at both methods with examples.
#include <stdio.h>
// Function definition
int multiply(int a, int b) {
return a * b;
}
int main() {
// Declare a function pointer and reference it to the multiply function
int (*multiply_ptr)(int, int) = multiply;
// Implicitly dereferencing the function pointer to call the function
int result = multiply_ptr(5, 6);
printf("Multiplication result: %d\n", result); // Output: 30
return 0;
}
Explanation:
Output:
Multiplication result: 30
#include <stdio.h>
// Function definition
int subtract(int a, int b) {
return a - b;
}
int main() {
// Declare a function pointer and reference it to the subtract function
int (*subtract_ptr)(int, int) = subtract;
// Explicitly dereferencing the function pointer
int result = (*subtract_ptr)(10, 4);
printf("Subtraction result: %d\n", result); // Output: 6
return 0;
}
Explanation:
Output:
Subtraction result: 6
In both examples, we reference the function using the function name (without parentheses) and call the function using the function pointer. The only difference is in the dereferencing method—explicitly using * or implicitly calling the pointer as the function.
While referencing and dereferencing function pointers seem straightforward, there are some common pitfalls to watch out for:
1. Uninitialized Function Pointers:
Dereferencing an uninitialized function pointer can lead to undefined behavior and crashes.
Example of Mistake:
int (*func_ptr)(int, int); // Uninitialized function pointer
int result = func_ptr(3, 4); // This is dangerous - func_ptr is not initialized!
Fix: Always initialize function pointers before dereferencing them.
2. Function Pointer Type Mismatch
The pointer’s signature must match the function signature in terms of the return type and parameters. A mismatch will lead to errors or undefined behavior.
Example of Mistake:
void (*func_ptr)(int, int); // Function pointer expects void return type
func_ptr = add; // This is incorrect if add returns int, not void.
Fix: Ensure the pointer type matches the function type exactly.
3. Dereferencing NULL Function Pointers:
Trying to dereference a NULL function pointer will lead to segmentation faults or crashes.
Example:
int (*func_ptr)(int, int) = NULL; // NULL function pointer
int result = func_ptr(3, 4); // Dereferencing NULL function pointer will crash
Fix: Always check if the function pointer is NULL before calling it:
if (func_ptr != NULL) {
int result = func_ptr(3, 4);
}
In more complex systems, function pointers can be stored in arrays or structures. This enables dynamic selection of functions based on runtime conditions.
#include <stdio.h>
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int main() {
// Array of function pointers
int (*operations[3])(int, int) = {add, subtract, multiply};
// Call functions through the array of pointers
printf("Addition: %d\n", operations[0](5, 3)); // Output: 8
printf("Subtraction: %d\n", operations[1](5, 3)); // Output: 2
printf("Multiplication: %d\n", operations[2](5, 3)); // Output: 15
return 0;
}
Explanation:
Output:
Addition: 8
Subtraction: 2
Multiplication: 15
Also Read: What is Array in C? With Examples
Referencing and dereferencing function pointers are critical for using them effectively in C. By understanding the syntax and the potential pitfalls, you can use them for dynamic function calls, callbacks, and more flexible, modular programming.
Passing pointers to functions in C is a fundamental concept that allows functions to manipulate data stored outside their local scope. It is widely used in dynamic memory allocation, function callbacks, modifying function arguments, and optimizing performance.
Passing pointers to functions provides several advantages:
The general syntax of passing a pointer to a function is:
return_type function_name(data_type *pointer_name) { // Function code}
Example Syntax:
void modifyValue(int *ptr) { *ptr = 20; // Modifies the value stored at the address}
Here:
When passing an array to a function, we typically pass its pointer, as arrays decay into pointers when passed to functions.
#include <stdio.h>
void printArray(int *arr, int size) {
printf("Array elements: ");
for(int i = 0; i < size; i++) {
printf("%d ", *(arr + i)); // Accessing elements using pointer arithmetic
}
printf("\n");
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
printArray(numbers, size); // Passing the array (decays to a pointer)
return 0;
}
Explanation:
Output:
Array elements: 1 2 3 4 5
Structures in C store multiple data types under one entity. Passing pointers to structures is a common practice to avoid unnecessary data copying.
Example: Function Modifying a Structure Using Pointer
#include <stdio.h>
struct Student {
char name[50];
int age;
};
// Function that modifies structure using pointer
void updateStudent(struct Student *s) {
s->age += 1; // Modify age using -> operator
}
int main() {
struct Student student1 = {"Alice", 20};
printf("Before update: %s is %d years old\n", student1.name, student1.age);
updateStudent(&student1); // Passing structure pointer
printf("After update: %s is %d years old\n", student1.name, student1.age);
return 0;
}
Explanation:
Output:
Before update: Alice is 20 years old
After update: Alice is 21 years old
Also Read: Data Types in C and C++ Explained for Beginners
Function pointers allow functions to be passed as arguments, making the code more flexible.
Example: Function Pointer as Argument
#include <stdio.h>
// Function definitions
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }
// Function taking function pointer as argument
void operate(int x, int y, int (*operation)(int, int)) {
printf("Result: %d\n", operation(x, y)); // Calls the passed function
}
int main() {
operate(5, 3, add); // Calls add(5,3)
operate(5, 3, multiply); // Calls multiply(5,3)
return 0;
}
Explanation:
Output:
Result: 8Result: 15
Passing pointers to functions in C is an essential technique that enhances flexibility, performance, and efficiency. It allows modifying variables outside the function scope, prevents unnecessary data copying, and supports dynamic function calls.
In C programming, function pointers are extremely powerful tools, enabling dynamic behavior in code. One of the most popular use cases of function pointers is callback functions, where a function pointer is passed as an argument to another function.
This pattern is essential in situations such as event handling, asynchronous programming, and dynamic function execution, making the code more flexible and reusable.
A callback function is simply a function that is passed as an argument to another function. The receiving function can then invoke the callback function at an appropriate time, providing dynamic functionality.
For example, in GUI (Graphical User Interface) libraries, the user might click a button, and the program needs to call a specific function (callback) to handle the event. This concept is also prevalent in sorting algorithms or signal handling.
In C, a callback function is implemented by passing a function pointer to another function. The receiving function can then use the function pointer to call the callback function dynamically without knowing which exact function it will be. This is what makes function pointers and callbacks so versatile.
Here’s how a callback pattern works:
Let’s start with a simple example of using a function pointer as a callback in C. We will define a function ‘performOperation’ that accepts another function as a parameter to be used as a callback.
Example Code:
#include <stdio.h>
// Callback function signatures
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
// Function that accepts a callback function
void performOperation(int a, int b, int (*operation)(int, int)) {
int result = operation(a, b); // Call the passed-in function
printf("The result of the operation is: %d\n", result);
}
int main() {
int x = 10, y = 5;
// Passing different callback functions
performOperation(x, y, add); // Callback for addition
performOperation(x, y, multiply); // Callback for multiplication
return 0;
}
Explanation:
Output:
The result of the operation is: 15 // Result of add(x, y)
The result of the operation is: 50 // Result of multiply(x, y)
Another classic example of a callback function is using function pointers to implement sorting algorithms. Here’s an example of sorting an array using a callback to compare the elements.
#include <stdio.h>
#include <stdlib.h>
// Comparison function for ascending order
int ascending(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
// Comparison function for descending order
int descending(const void *a, const void *b) {
return (*(int*)b - *(int*)a);
}
// Function to sort an array with a callback function
void sortArray(int *arr, int size, int (*compare)(const void*, const void*)) {
qsort(arr, size, sizeof(int), compare); // Use qsort with callback comparison function
}
int main() {
int arr[] = {4, 2, 7, 1, 9, 3};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Sorting in ascending order
sortArray(arr, size, ascending);
printf("Sorted array (ascending): ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Sorting in descending order
sortArray(arr, size, descending);
printf("Sorted array (descending): ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Explanation:
Output:
Original array: 4 2 7 1 9 3
Sorted array (ascending): 1 2 3 4 7 9
Sorted array (descending): 9 7 4 3 2 1
Here are some of the standard applications of callback functions you must know:
1. Event Handling:
In graphical user interfaces or embedded systems, callback functions are used extensively for event handling. When a user clicks a button, a specific function (callback) is invoked to handle the event.
2. Asynchronous Programming:
Callbacks are used in asynchronous programming to handle tasks like reading from a file, network communication, or database queries. The program doesn't wait for these tasks to be completed but instead registers a callback that gets executed once the task is finished.
3. Custom Sorting Algorithms:
As demonstrated earlier, callback functions enable custom sorting behaviors. You can pass different comparison functions to sort data in various ways.
4. Timer Functions:
Many real-time operating systems use callback functions for time-triggered tasks. The callback is registered to be executed when a certain time has elapsed, or a condition has been met.
There you go! You can implement these features by using function pointers and callbacks. These techniques provide flexibility and can drastically improve the readability and maintainability of your code.
Also Read: Top 3 Open Source Projects for C [For Beginners To Try]
Optimizing code for performance is a critical part of software development, especially in languages like C, where low-level memory management is directly in the hands of the developer.
One of the most effective optimization techniques in C is using function pointers. Function pointers allow dynamic selection of functions, enabling more efficient code, better modularity, and reduced redundancy.
So, let’s look at some common scenarios where you can use function pointers for effective optimization.
When a similar operation is needed for different data types or conditions, you could have redundant functions. Instead of writing separate functions for each case, you can use function pointers to create a generic function that works with all cases.
Example: Using Function Pointers to Reduce Redundant Operations
Suppose you need to perform mathematical operations (like addition, subtraction, multiplication) on two integers, but rather than writing separate functions for each, you can use function pointers to handle different operations dynamically.
#include <stdio.h>
// Define operation functions
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
// Generic function to perform operations
void performOperation(int x, int y, int (*operation)(int, int)) {
int result = operation(x, y);
printf("Result: %d\n", result);
}
int main() {
int a = 10, b = 5;
// Using function pointers to reduce redundancy
performOperation(a, b, add);
performOperation(a, b, subtract);
performOperation(a, b, multiply);
return 0;
}
Explanation:
Output:
Result: 15 // Addition result
Result: 5 // Subtraction result
Result: 50 // Multiplication result
This simple approach significantly reduces code duplication and improves maintainability, especially if the operation functions grow in number or complexity.
One of the most potent uses of function pointers is the ability to select functions at runtime dynamically. This is useful when your program's behavior needs to change based on user input, external data, or other runtime conditions.
Example: Dynamic Function Selection Based on User Input
Imagine a scenario where you need to handle different sorting algorithms based on user choice. Instead of using multiple if-else or switch statements to select the algorithm, function pointers can make the selection process cleaner and more efficient.
#include <stdio.h>
#include <stdlib.h>
// Sorting functions
int compareAsc(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int compareDesc(const void *a, const void *b) {
return (*(int*)b - *(int*)a);
}
// Function to select sorting algorithm
void sortArray(int *arr, int size, int (*compare)(const void*, const void*)) {
qsort(arr, size, sizeof(int), compare);
}
int main() {
int arr[] = {4, 2, 9, 1, 5};
int size = sizeof(arr) / sizeof(arr[0]);
// Ask user for sorting order
int choice;
printf("Enter 1 for Ascending or 2 for Descending order: ");
scanf("%d", &choice);
// Function pointer to select sorting algorithm
if (choice == 1) {
sortArray(arr, size, compareAsc);
} else if (choice == 2) {
sortArray(arr, size, compareDesc);
}
printf("Sorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Explanation:
Output:
Enter 1 for Ascending or 2 for Descending order: 1
Sorted array: 1 2 4 5 9
This dynamic function selection reduces unnecessary condition checks, making the code more efficient and easier to expand (e.g., adding more sorting algorithms).
By using function pointers, you can also increase the modularity of your code, making it easier to maintain and extend. This is especially useful in large projects where certain behaviors might need to change, and function pointers can be used to swap out entire blocks of functionality without rewriting the core logic.
Example: Modularizing Event Handling with Function Pointers
Consider a scenario where you have different event handlers for different actions in a game or a GUI. Instead of writing a large switch case or nested if-else statements for each event, you can use function pointers to assign handlers dynamically.
#include <stdio.h>
// Define event handler functions
void onKeyPress() {
printf("Key pressed event triggered\n");
}
void onMouseClick() {
printf("Mouse clicked event triggered\n");
}
// Function that accepts an event handler callback
void triggerEvent(void (*eventHandler)()) {
eventHandler(); // Call the event handler function
}
int main() {
// Event handling based on some conditions
int event = 1; // Assume 1 is key press, 2 is mouse click
if (event == 1) {
triggerEvent(onKeyPress); // Trigger key press event
} else if (event == 2) {
triggerEvent(onMouseClick); // Trigger mouse click event
}
return 0;
}
Explanation:
Output:
Key pressed event triggered
This approach improves maintainability by keeping event handlers modular and easy to extend.
While using function pointers generally provides flexibility and reduces code complexity, in some cases, it may also lead to performance improvements. This is particularly true in cases where you need to make runtime decisions about which function to call and where using a direct function call might introduce unnecessary checks.
For example:
By embracing function pointers for optimization, you can write cleaner, more efficient, and more maintainable code, which is crucial for both small and large software projects.
Function pointers are a powerful feature in C, but they come with their own set of challenges. Improper usage can lead to subtle bugs, crashes, and undefined behavior.
Let’s understand some of the most frequent mistakes, their causes, and how to avoid them.
1. Forgetting to Initialize a Function Pointer
One of the most common mistakes when working with function pointers is forgetting to initialize them. An uninitialized function pointer holds an unknown memory address, which can lead to undefined behavior or a segmentation fault if dereferenced.
Solution: Always initialize your function pointers. You can either assign them directly to a function or set them to NULL and check if they are NULL before calling.
2. Incorrect Function Pointer Type or Signature
C is a strongly typed language, and mismatched types between the function pointer and the actual function can cause undefined behavior or compilation errors. This is particularly problematic when dealing with complex signatures or function overloading.
Solution: Always ensure the function pointer matches the exact signature of the function it points to. If the function takes two parameters, the pointer must match that exactly.
3. Passing Function Pointers with Incorrect Signatures
When passing function pointers as arguments, it's easy to mix up the function signature or forget to cast the pointer appropriately. This can lead to undefined behavior or incorrect function calls.
Solution: Ensure the function pointer passed to another function matches the expected signature. If necessary, use casting to match the types explicitly.
4. Using Function Pointers with Different Calling Conventions
Different compilers or platforms might use different calling conventions (the method used to pass arguments to functions). Mismatching calling conventions between the function and function pointer type can cause crashes or stack corruption.
Solution: Ensure that the calling conventions match the platform's expected conventions. For most scenarios in C, you should stick with the standard ‘cdecl’ or ‘stdcall’ conventions, depending on the compiler.
By avoiding these common mistakes, you can utilize the full potential of function pointers without introducing bugs into your C programs.
Test your understanding of function pointers in C with these 10 multiple-choice questions (MCQs). Each question covers different concepts related to function pointers, from syntax to advanced uses, ensuring a comprehensive review of the topic.
1. What is a function pointer in C?
a) A pointer that points to a variable
b) A pointer that holds the address of a function
c) A pointer that stores a string
d) A pointer used for dynamic memory allocation
2. Which is the correct way to declare a function pointer in C?
a) int (*ptr)(int, int);
b) int ptr(int, int);
c) int ptr(*)(int, int);
d) int (*ptr)(int, float);
3. How can you assign a function to a function pointer in C?
a) ptr = functionName;
b) ptr = &functionName;
c) ptr = &functionName();
d) ptr = functionName();
4. What will be the output of the following code?
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*operation)(int, int) = add;
printf("Result: %d\n", operation(5, 3));
return 0;
}
a) Result: 8
b) Result: 5
c) Result: 15
d) Result: Undefined
5. What is the correct way to call a function via a pointer?
a) *ptr();
b) ptr();
c) (*ptr)();
d) &ptr();
6. Which of the following statements about function pointers is false?
a) Function pointers can be passed as arguments to other functions.
b) Function pointers can point to any function that matches its signature.
c) Function pointers can only point to functions in the same file.
d) Function pointers can be assigned to variables dynamically.
7. What is a major risk when using function pointers in C?
a) Increased memory usage
b) Loss of data precision
c) Dereferencing uninitialized or NULL function pointers
d) Reduced code maintainability
8. What happens if you dereference a NULL function pointer in C?
a) The program executes successfully
b) A segmentation fault occurs
c) The program continues but produces incorrect results
d) It causes an infinite loop
9. What is the advantage of using function pointers in a table-driven design?
a) It helps avoid the use of loops
b) It allows dynamic selection of functions at runtime
c) It makes code more readable by using more variables
d) It prevents the use of conditional statements
10. What will be the output of the following program?
#include <stdio.h>
void greet() {
printf("Hello!\n");
}
void farewell() {
printf("Goodbye!\n");
}
void execute(void (*func_ptr)()) {
func_ptr();
}
int main() {
void (*greeting_ptr)() = greet;
void (*farewell_ptr)() = farewell;
execute(greeting_ptr);
execute(farewell_ptr);
return 0;
}
a) Hello! Goodbye!
b) Goodbye! Hello!
c) Hello!
d) Goodbye!
These questions will help reinforce the key ideas discussed and deepen your understanding of how to use function pointers effectively in your programs.
upGrad’s courses provide in-depth training in C programming, covering fundamental concepts like loops, recursion, functions, and pointers. You’ll gain hands-on experience writing efficient C programs, including factorial calculations, data structures, and algorithm optimization.
By learning C programming, you’ll build a strong foundation for software development, competitive programming, and system-level programming.
Check out some of these upGrad courses to take your programming skills to the next level!
You can also get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today!
Similar Reads:
Explore C Tutorials: From Beginner Concepts to Advanced Techniques
Addition of Two Numbers in C: A Comprehensive Guide to C Programming
C Program to check Armstrong Number
Array in C: Introduction, Declaration, Initialisation and More
Exploring Array of Pointers in C: A Beginner's Guide
What is C Function Call Stack: A Complete Tutorial
Discovering C Operators: An Overview with Types and Examples!
Constant Pointer in C: The Fundamentals and Best Practices
Dangling Pointer in C: Causes, Risks, and Prevention Techniques
Find Out About Data Structures in C and How to Use Them?
C Program to Convert Decimal to Binary
Function pointers are used for dynamic function calls, callback functions, event handling, implementing state machines, and optimizing code by reducing redundant conditional statements.
Unlike regular pointers that store memory addresses of variables, function pointers store addresses of functions and allow indirect function calls.
A callback function is a function that is passed as an argument to another function and executed later. It is widely used in event-driven programming, sorting functions, and handling asynchronous operations.
Common mistakes include using uninitialized function pointers, mismatched function signatures, dereferencing NULL function pointers, incorrect casting, and not checking function pointer validity before calling.
Function pointers reduce redundant code by allowing dynamic function selection at runtime, minimize conditionals (if-else, switch statements), and enhance modularity in large applications.
Yes, function pointers can be stored in arrays to facilitate quick function selection, such as in table-driven programming, where different behaviors are dynamically assigned.
No, function pointers in C are not type-safe. If a function pointer is assigned to a function with a mismatched signature, it can lead to undefined behavior, so careful type-checking is necessary.
Function pointers allow code to be more flexible and maintainable by enabling interchangeable behavior without modifying core logic, making functions reusable and adaptable.
Yes, as long as the function is declared properly in a header file or with extern linkage, function pointers can point to functions defined in other source files.
In many cases, yes. Function pointers eliminate the overhead of checking multiple conditions and provide a direct jump to the required function, improving execution speed.
Avoid using function pointers when simple function calls or direct conditional branching (if-else, switch) are more readable. Excessive use of function pointers can make debugging harder and reduce code clarity if not well-documented.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.