Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2 — Function Pointers and Advanced Pointers

Lesson 28 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Function Pointers and Advanced Pointers in C

Function Pointers

A function pointer stores the address of a function and allows calling it indirectly.

Syntax:

return_type (*pointer_name)(parameter_types);

Example:

#include <stdio.h>

int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

int main() {
    int (*op)(int, int);   /* function pointer */
    
    op = add;
    printf("Add: %d\n", op(3, 4));   /* 7 */
    
    op = mul;
    printf("Mul: %d\n", op(3, 4));   /* 12 */
    
    return 0;
}

---

Array of Function Pointers

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }

int (*ops[3])(int, int) = {add, sub, mul};
char *names[] = {"Add", "Sub", "Mul"};

for (int i = 0; i < 3; i++) {
    printf("%s(5,3) = %d\n", names[i], ops[i](5, 3));
}

---

Passing Function Pointer to Another Function

void apply(int *arr, int n, int (*transform)(int)) {
    for (int i = 0; i < n; i++) {
        arr[i] = transform(arr[i]);
    }
}

int square(int x) { return x * x; }
int doubleIt(int x) { return x * 2; }

int arr[] = {1, 2, 3, 4};
apply(arr, 4, square);   /* {1, 4, 9, 16} */

---

Pointer to Array vs Array of Pointers

int arr[5] = {1, 2, 3, 4, 5};

int (*ptr)[5] = &arr;     /* pointer to entire array */
printf("%d\n", (*ptr)[2]);  /* 3 */

int *ptrs[5];             /* array of 5 int pointers */
for (int i = 0; i < 5; i++) ptrs[i] = &arr[i];

---

Pointer to Pointer (Double Pointer)

int a = 10;
int *p = &a;
int **pp = &p;

printf("%d\n", **pp);   /* 10 */
*p = 20;
printf("%d\n", a);      /* 20 */

Use case — dynamic 2D array:

int rows = 3, cols = 3;
int **matrix = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++)
    matrix[i] = (int *)malloc(cols * sizeof(int));

matrix[0][0] = 1;
matrix[2][2] = 9;

for (int i = 0; i < rows; i++) free(matrix[i]);
free(matrix);