Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Pointer declaration error (http://www.go4expert.com/forums/pointer-declaration-error-t24423/)

bsudhir6 7Jan2011 12:43

Pointer declaration error
 
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

jimblumberg 7Jan2011 21:03

Re: Pointer declaration error
 
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

bsudhir6 8Jan2011 17:05

Re: Pointer declaration error
 
Thanks for the i learnt that i should declare a function pointer in order to point a function


All times are GMT +5.5. The time now is 02:38.