Function Pointers in C / C++



See complete series on pointers here:

In this lesson, we have explained the concept of function pointers and shown how you can create and use function pointers in C or C++.

Feel free to drop your comments, feedback and suggestions.

For practice problems and more, visit: http://www.mycodeschool.com

Like us on Facebook: https://www.facebook.com/MyCodeSchool

Follow us on twitter: https://twitter.com/mycodeschool

27 thoughts on “Function Pointers in C / C++

  1. I wrote the same Add function and printed the value of Add (cout<<Add;). I am getting a value of 1. Isn't that supposed to be the starting address of the function? Even when I write another function below it and print its address, I still get 1. Could anyone explain?

  2. #include<stdio.h>

    #include<stdlib.h>

    void read(int *arr,int n)

    {

    printf("Enter the elements of the array:n");

    int i;

    for(i=0;i<n;i++)

    {

    printf("arr[%d] = ",i);

    scanf("%d",&arr[i]);

    }

    }

    void show(int *arr,int n)

    {

    int i=0;

    printf("nnShow the elements of the arrayn");

    for(i=0;i<n;i++)

    {

    printf("%dt",arr[i]);

    }

    }

    int *biggest(int *arr,int n)

    {

    int *large=(int*)malloc(sizeof(int));

    large=arr;

    int i;

    for(i=0;i<n;i++)

    {

    if(arr[i]>*large)

    {

    *large=arr[i];

    }

    }

    return large;

    free(large);

    }

    int main()

    {

    int *arr,n;

    printf("Enter the size of the array:");

    scanf("%d",&n);

    void (*p)(int*,int)=&read;

    (*p)(arr,n);

    void (*q)(int*,int)=&show;

    (*q)(arr,n);

    int* (*m)(int*,int)=&biggest;

    int *large=(*m)(arr,n);

    printf("nnLargest element is =%dn",*large);

    }

  3. You explain everything in a very simple and easy way to understand. You are really great and talented. It would be great if you could make a c++ full course that covers all the c++ language. For me it's much better than learning at the university from all teachers. Thank you so much. Please do the full course.

  4. I'm reading c/c++ from class 11th, now I'm in Btech 4th year and now I'm knowing first time that there is something called pointer to a function and function name denotes function address.

  5. Hello, i have the same problem to understand void (*ptr)(char*). pointer function expects for an address as argument, but when it gets the address of "Tom", should in void function as output like this :printf("Hello %sn" , *name); why here no * for name. thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *