Thread: Callbacks in c

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Callbacks in c


    hello i have a problem with callbacks. i dont understand how can i call a function using only the functions name without sending any parameters, while the function prototype has parameters. For example

    int fun(int param1, int param2){

    }

    int main(){
    funcall=fun;


    }

    how the calculations will be done?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Originally Posted by mcact
    int main(){
    funcall=fun;


    }
    You are not calling function fun here with this code. If you want to call it, you should put parens around it:
    Code:
    int main(void) {
        funcall = fun();
    }
    and it will crib that you didn't supply enough parameters.

    With what you have currently, it simply takes the address of the code for the function fun and assigns it to funcall. Try turning your compiler warnings on to the highest level and you should see it complain about converting pointers to int without casting.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Pointers just contain addresses, but what they are declared as pointing to is very important. When you dereference or increment it, an int pointer is very different from a double pointer is very different from pointers to all kinds of structs from pointers to all kinds of objects (in C++; C does not support OOP directly). And they are all different from function pointers. And one kind of function pointer is different from all the other kinds of function pointers.

    When you declare a function pointer, part of the information you give it is the parameter list types and the return type. If you want a function pointer that would have a different parameter list, then you would need to declare another function pointer type for it. Then when you dereference a function pointer, that is when you give it the arguments.

    Read up on function pointers for the syntax of declaring function pointers and of dereferencing them.

    And the next time you offer a code example, be absolutely certain to include type declarations. For example, in the example you gave, how is funcall declared? As it stands (ie, being used with having been declared), it would default to int, which is not compatible with a function pointer type.

IMN logo majestic logo threadwatch logo seochat tools logo