Advanced C Pointers: Dynamic Memory Allocation, Arrays of Pointers, Function Pointers, and More
🔹 Introduction
In C, mastering basic pointers is only the beginning. The real power lies in dynamic memory allocation, arrays of pointers, and even passing functions as parameters. These advanced concepts bring flexibility, efficiency, and real-world utility to your C code.
🔹 Dynamic Memory Allocation in C
Unlike static memory (like int a[10]), dynamic memory lets you decide at runtime how much space your program needs.
✅ Functions Used:
✅ Example 1: malloc() for Single Integer
#include <stdio.h> #include <stdlib.h> int main() { int *ptr = (int *)malloc(sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.\n"); return 1; } *ptr = 42; printf("Value: %d\n", *ptr); free(ptr); return 0; }
Value: 42
✅ Example 2: calloc() for Array
#include <stdio.h> #include <stdlib.h> int main() { int *arr = (int *)calloc(5, sizeof(int)); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); // All will be 0 } free(arr); return 0; }
0 0 0 0 0
✅ Example 3: realloc() to Resize Memory
#include <stdio.h> #include <stdlib.h> int main() { int *arr = malloc(2 * sizeof(int)); arr[0] = 10; arr[1] = 20; arr = realloc(arr, 4 * sizeof(int)); arr[2] = 30; arr[3] = 40; for (int i = 0; i < 4; i++) { printf("%d ", arr[i]); } free(arr); return 0; }
10 20 30 40
🔹 Arrays of Pointers
Instead of one big array, you can have an array of individual pointers pointing to different data. Common use: array of strings.
✅ Example 4: Array of Strings Using Pointers
#include <stdio.h> int main() { char *fruits[] = {"Apple", "Banana", "Cherry"}; for (int i = 0; i < 3; i++) { printf("%s\n", fruits[i]); } return 0; }
Apple
Banana
Cherry
🔹 Pointer to Pointer
char **ptr
is a pointer to another pointer.
#include <stdio.h> int main() { char *msg = "Hello"; char **p = &msg; printf("Value: %s\n", *p); return 0; }
Value: Hello
🔹 Function Pointers
Function pointers let you store functions in variables and pass them around like data.
✅ Syntax:
return_type (*func_ptr)(param_type) = function_name;
✅ Example 5: Using Function Pointer
#include <stdio.h> void greet() { printf("Hello from function pointer!\n"); } int main() { void (*fptr)() = greet; fptr(); // Calls greet return 0; }
Hello from function pointer!
🔹 Passing Functions to Other Functions
This is also called a callback function.
✅ Example 6: Callback in Processing
#include <stdio.h> void process(int x, void (*f)(int)) { f(x); } void square(int n) { printf("Square: %d\n", n * n); } int main() { process(5, square); // Passing function return 0; }
Square: 25
🔹 Pointer Declaration Tricks
🧠Variants:
✅ Example 7: Menu System Using Function Pointers
#include <stdio.h> void add() { printf("Add\n"); } void edit() { printf("Edit\n"); } void delete() { printf("Delete\n"); } int main() { void (*menu[])(void) = {add, edit, delete}; int choice; printf("Enter 0 (Add), 1 (Edit), 2 (Delete): "); scanf("%d", &choice); if (choice >= 0 && choice < 3) menu[choice](); else printf("Invalid choice"); return 0; }
Edit
🔹 Common Pitfalls
✅ Conclusion
From dynamic memory to function pointers, advanced pointer techniques unlock incredible power in C. They're not just theoretical—they're used in OS development, drivers, embedded systems, and performance-critical applications.
❓ Frequently Asked Questions
1. What's the difference between malloc and calloc?
malloc() allocates garbage (uninitialized) memory. calloc() initializes to zero.
2. Can you have an array of function pointers?
Yes, very useful for menu-driven or command-based programs.
3. What is a callback function?
A function passed as a parameter to another function.
4. How do you avoid memory leaks?
Always free() memory after use. Avoid overwriting pointers before freeing them.
5. What's the use of pointer-to-pointer?
Used in dynamic 2D arrays, or handling arrays of strings, or function arguments that modify a pointer.
0 Comments
If you have any doubts, Please let me know