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

    Join Date
    Oct 2002
    Posts
    32
    Rep Power
    13

    Function pointers...


    How should I declare a pointer to a function? (like I declare variables in a code segment...)

    Also, can i assign a function name to a pointer like this:

    float (*funcPtr)(float, float) = myFunctionName;
    posted by: justujoo

    Error 13: BRAIN.SYS not responding, process terminted...!
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    your example might work as is, i'm not sure. if it doesn't it's very close to being correct - the only thing you've got to do is to split it up into two lines..

    basically whatever your function looks like, you need to follow it's decleration closely in the way your define the pointer to it - it's return types and argument types.

    so if you've got a function like say:

    Code:
    float function(int x, char *x, float x)
    {
    	....
    	....
    }
    then you'd define a pointer to it like:

    Code:
    float (*funcpointer)(int x, char *x, float x);
    funcpointer = function;
    (variable names optional (i think), but not the types)

    so now you've got function's address in a pointer called funcpointer.

    you can then call that function using the pointer like:
    Code:
    *funcpointer(anint, pointertochar, afloat);
    so your example would be:
    Code:
    float (*funcPtr)(float, float);
    funcPtr = myFunctionName;
    then
    Code:
    *funcPtr(afloat, afloat);
    to call it
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    32
    Rep Power
    13
    Thanks alot balance.

    variable names optional (i think),
    Definately the variable names are optional (as for as the prototype/declaration is concerned)

    the only thing you've got to do is to split it up into two lines
    Now my point here is that I can initialize a pointer in the same statement in which i declare it i.e. :

    int *iPtr = &someIntVariable;

    so why do We've to do function pointer initialization in a new statment?
    posted by: justujoo

    Error 13: BRAIN.SYS not responding, process terminted...!
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    Code:
    #include <stdio.h>
    
    void print(char *, int);
    void (*prints)();
    
    main()
    {
            prints = &print;
            prints("Hello World!", 10);
    }
    
    void print(char *s, int i)
    {
            printf("%s %d\n",s, i);
    }
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    so why do We've to do function pointer initialization in a new statment?
    you don't, i don't think, if it works that is.

    i guess it's like declaring and defining a variable in the same line like:

    int x = 5;

    as opposed to:

    int x;
    x = 5;

    kill two birds with one stone type thing

    obviously having the option to assign later, in general, is essential, but how you assign it in the first place won't make much difference either way i don't think.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    32
    Rep Power
    13
    MarkMan's code is wrong...

    You've to consider the arguments' types of the function as well when you declare a pointer to that function, so the correct function pointer initialization will be:

    void (*prints)(char *, int);

    Thanks balance, but why then did you say that I'd to break the code into two line ;)
  12. #7
  13. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    Thanks balance, but why then did you say that I'd to break the code into two line
    i said:
    your example might work as is, i'm not sure. *if it doesn't* it's very close to being correct - the only thing you've got to do is to split it up into two lines..

IMN logo majestic logo threadwatch logo seochat tools logo