Pointer declaration error

bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
where is the wrong code???
Code:
#include<stdio.h>

func()
{
    return printf("Hi");
}
main()
{
    void *fptr=func;
    (*fptr)();
}
errors by visual c++ 2005 express compiler displayed are:
Code:
at line number 11:
error C2100: illegal indirection
error C2064: term does not evaluate to a function taking -22
errors by turbo c compiler are
Code:
"Not an allowed type" at line number 10
0
jimblumberg's Avatar
Ambitious contributor
First you are trying to create a function pointer to a void function but since you don't state the return type of your function it defaults to returning an int.

And since you are returning a value it should be:

Code:
 int funct(void)
So now in main instead of trying to make a pointer to a function returning nothing you should be trying to make a pointer to a function returning an int.

Second in main() (which also should be int main() and return an int) you are just creating a pointer variable, not a pointer to a function. To create a pointer to a function you must include the (). Then you must de-reference the pointer when assigning the value.

So what you need to do is:

Code:
#include<stdio.h>

int func()
{
    return printf("Hi");
}

int main()
{
    int mretunValue
    int (*fptr)();
    fptr = &func;
    mreturnValue = (*fptr)();

    return(0);
}
See this link Function Pointers.

Jim
0
bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
Thanks for the i learnt that i should declare a function pointer in order to point a function