Function Pointers I

It is a long time since i have written C++ code, i admit that it is too hard to leave this language and continue coding with C#, Java or Python, C++ has its own Magic 😉 😀

In this post, i am trying to refresh my C++ memory, and i think there is nothing better than playing with some Pointers 😀
I will talk about “Function Pointers”, actually we aren’t use them in projects or problem solving, but they are something that are worth to be mentioned when we talk about pointers.

We all know that pointer is a variable that points to another variable and holds its address, function pointer is the same but instead of pointing to a variable, it points to a function.

Function Pointer declaration


int (*func_ptr) (); // a function pointer that takes no arguments and returns an integer value

We declare a function pointer like that with parenthesis around func_ptr, because if we do that int *func_ptr() it will be treated as a function that returns a pointer to an interger.

Like variable pointers, function pointers can point to other data types functions …


long (*func_ptr_long) (); // function pointer that points to a long function

double (*func_ptr_doub) (); // function pointer that points to a double function

Declaring a function pointer depends on the function that it points to it and the number of arguments that that function has ..


int (*func_ptr) (); // a function pointer that points to function that has no arguments

int (*func_ptr1) (int); // a function pointer that points to a function that has 1 argument which is an integer variable

int (*func_ptr2) (int, int); // a function pointer that points to a function that has 2 arguments which are integer variables

Assigning a function to a function pointer


#include <stdio.h>

int func () { }

int main() {
    int (*func_ptr) () = func; // Assigning the func to the func_ptr

    return 0;
}

Remember, when you declare a function pointer you should make sure that its data type andnumber of arguments are the same to the function which it will points to


#include <stdio.h>

int func () {}

int func2 (int x) {}

int main() {
    double (*func_ptr) () = func; // Error, the data types aren't the same
    int (*func_ptr2) () = func2; // Error, the number of arguments are different

    return 0;
}

Calling a function using a function pointer

There are two ways to call a function using a pointer

Explicit dereference


#include <stdio.h>

int func (int a) {}

int main() {
    int (*func_ptr) (int) = func; // Assigning
    (*func_ptr) (9); // Calling the function

    return 0;
}

Implicit dereference


#include <stdio.h>

int func (int a) {}

int main() {
    int (*func_ptr) (int) = func; // Assigning
    func_ptr(9); // Calling the function

    return 0;
}

Implicit dereference is like calling a normal function as you see.

Example on using a function pointer to call a function then print its returned value.


#include <stdio.h>

int Sum (int a, int b) {
    return a + b;
}

int main() {
    int (*func_ptr) (int, int) = Sum; // Assigning the Sum function to a pointer
    int Result = func_ptr(3, 4); // Storing the returned value when calling the Sum function
    printf("%d\n", Result) ; // output : 7

    return 0;
}

Function pointer and Overloading

Function pointers support overloading …


#include <stdio.h>

int func(int a) {
return a + 1;
}

int func(int a, int b) {
return a + b;
}

int main() {
int (*func_ptr1) (int);
int (*func_ptr2) (int, int);
func_ptr1 = func; // func_ptr1 points to func(int)
func_ptr2 = func; // func_ptr2 points to func(int, int)

return 0;
}

Function pointer in memory

Like a variable pointer, function pointer points into memory, but the only difference that variable points directly to an address which already has data, but function pointer points to memory after the execution of the function.

Why using function pointers ?

You can use function pointers when you implement a function that you could change its behavior during your implementation without writing an exhaustive code.

Here is an example of using a function pointer to change the behavior of Bubble sort algorithm easily …


#include <stdio.h>

int Arr[] = {2, 3, 1, 6, 2, 0, 0, 1};

void BubbleSort(int Length, bool (*func_ptr) (int, int)) {

for(int i = 0; i < Length - 1; i++) {
    for(int j = 0; j < Length - i - 1; j++) {
        if(func_ptr(Arr[j], Arr[j + 1]) == true) {
           int Tmp = Arr[j];
           Arr[j] = Arr[j + 1];
           Arr[j + 1] = Tmp;
        }
     }
   }
}

inline bool Increasing(int First, int Second) {
    return First > Second;
}

inline bool Decreasing(int First, int Second) {
    return First < Second;
}

int main() {
    BubbleSort(8, Increasing);
    for(int i = 0; i < 8; i++) {
        printf("%d\n", Arr[i]);
    }
    puts("----");
    BubbleSort(8, Decreasing);
    for(int i = 0; i < 8; i++) {
        printf("%d\n", Arr[i]);
  }

 return 0;
}

Well, Hope Function pointers now becomes clear enough and i know that you can use Virtual functions and Polymorphism to do the same job, maybe next topic we will discuss something that similar to function pointers in C++11 Lambda Functions.