× Home
Next Lec → ← Previous Lec

Function Pointers

POINTERS (Quick revision)

  • A pointer is a variable which stores address of another variable.
  • & symbol is used to get the address of a variable.
  • * symbol is used to get the value of the variable that the pointer is pointing to.
  • In C, we can create generic pointers too.
  • Regular C variable stores the value whereas pointer stores the address of the variable.

Dynamic memory allocation : Recap

  • An statically allocated variable or array has fixed size in memory.
  • Dynamic Memory Allocation is a way in which the size of a data structure can be changed during the runtime.
  • Memroy assigned to a program in a typical architecture can be broken down into four segments:
    • Code
    • Static/global variables
    • Stack
    • Heap

Function Pointers

syntax:

                    
                        int (*p) (int, int); // p is a function pointer which have two parameter of type int.
                        p = &func1;
                    
                    

We are making a variable which is storing the address of instructions and using this address we can get to those instruction and we can execute that function.

                        

                        #include <stdio.h>
                         #include <stdlib.h>
                        
                         int sum(int a, int b)
                         {
                            return a+b;
                         }
                        
                         int main()
                         {
                            int (*fPtr) (int, int); // fPtr is going to point a function which returns int and takes two int.
                            fPtr = ∑
                            int d = (*fPtr)(4,6);
                            printf("The value of d is : %d",d);
                            return 0;
                         }
                        
                    

lec-73 Callback Functions Using Function Pointers

Function Pointers

Callback Functions

                        

                                #include <stdio.h>
                                #include <stdlib.h>
                                
                                int sum(int a, int b)
                                {
                                    return a + b;
                                }
                                void greetHelloAndExecute(int (*fptr)(int, int))
                                {
                                    printf("Hello user\n");
                                    printf("The sum of 5 and 7 is %d\n", fptr(5, 7));
                                }
                                
                                void greeGmAndExecute(int (*fptr)(int, int))
                                {
                                    printf("Good moring user\n");
                                    printf("The sum of 5 and 7 is %d\n", fptr(5, 7));
                                }
                                
                                int main()
                                {
                                    int (*ptr)(int, int);
                                    ptr = sum;
                                    greeGmAndExecute(ptr);
                                    greetHelloAndExecute(ptr);
                                    return 0;
                                }
                        
                    
                        

                                #include <stdio.h>
                                #include <stdlib.h>
                                
                                int calAvg(int a, int b)
                                {
                                    return ((a + b) / 2);
                                }
                                void greetGaAndExecute(int (*fptr)(int, int))
                                {
                                    printf("Good Afternoon\n");
                                    printf("The average of 10 and 4 is = %d\n", fptr(10, 4));
                                }
                                void greetGeAndExecute(int (*fptr)(int, int))
                                {
                                    printf("Good Evening\n");
                                    printf("The average of 10 and 4 is = %d\n", fptr(10, 4));
                                }
                                
                                int main()
                                {
                                    int (*avg)(int, int);
                                    avg = calAvg;
                                    greetGaAndExecute(avg);
                                    greetGeAndExecute(avg);
                                    return 0;
                                }