passing pointer to a function in c code example

Example 1: c function pointer in argument

void f(void (*a)()) {
    a();
}

void test() {
    printf("hello world\n");
}

int main() {
     f(&test);
     return 0;
}

Example 2: pass the pointer in C

// passing a pointer to function in C
// It increaments the salary of an employee


#include <stdio.h>
void salaryhike(int  *var, int b)
{
    *var = *var+b;
}
int main()
{
    int salary=0, bonus=0;
    printf("Enter the employee current salary:"); 
    scanf("%d", &salary);
    printf("Enter bonus:");
    scanf("%d", &bonus);
    salaryhike(&salary, bonus);
    printf("Final salary: %d", salary);
    return 0;
}

Tags:

C Example