Pointer declaration error

Discussion in 'C' started by bsudhir6, Jan 7, 2011.

  1. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    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
    
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    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
     
  3. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the i learnt that i should declare a function pointer in order to point a function
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice