function pointer that takes and returns a pointer

1792 views c

I'm trying to get this code to run..

#include <stdio.h> 

int *myMethod(int *a) 
   return a; 

int main() 
    // my_ptr is a pointer to function myMethod() 
    int *my_ptr(int *) = myMethod; 

    // Invoking myMethod() using my_ptr 

    int a = 5;
    int *p = (*my_ptr)(&a); 
    printf("Bye %d\n", *p);

    return 0; 

I thought my syntax for a function pointer, my_ptr, would be correct where it could take an int pointer as it's parameter and return an int pointer but when I compile it, I get the error that:

 error: function 'my_ptr' is initialized like a variable
 int *my_ptr(int *) = myMethod; 

Could someone explain the error/issue? Thanks!

answered question

1 Answer


int* my_ptr(int*) is the prototype of a function. You want a function pointer: int* (*my_ptr)(int*)

posted this

Have an answer?


Please login first before posting an answer.