Thread: Pointer to functions

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    27

    Pointer to functions

    Hi,

    How do I use default values in pointers to functions, say I want to do this,

    Code:
    typedef struct {
    void (*append)(int,int)
    } LOGVTBL;
    
    LOGVTBL log = {LogAppend};
    
    
    void LogAppend(int x, int y=10) {
    //some code
    }
    
    then I want to be able to do:
    
    logvt.append(10) ;
    logvt.append(10,15);
    Please help!

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Default arguments are C++?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    Is there any way of taking several arguments but only using the ones that are passed to the function?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,209
    There's a few alternative solutions:

    1) Create as many functions as you want to have a different number of arguments for. For example, you could make a func2() which takes 2 arguments, func4() which takes 4 arguments, etc.

    2) Use a variable arguments parameter like printf() does. Something like: return_type func(int numargs, ...); Then you'd just pass the number of arguments first like func(3, 10, 20, 30);

    3) Create one function that accepts the maximum number of arguments you're going to pass it. Then have a "use default" value for them. Say your function expects up to 3 positive integers. You could have a check to see if each argument is less than 0 and if it is you could set it to some default value. Like: if(arg2 < 0) arg2 = 8;

    You just have to be creative. Use a method that works best for you. Either that or switch to C++ where you have polymorphism.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    C99 allows function overloading

  6. #6
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    C99 allows function overloading
    Where does the C99 standard say that? I don't remember reading it anywhere, and I can't find it right now...but that could be the Guinness' fault.
    Just because I don't care doesn't mean I don't understand.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by sand_man
    C99 allows function overloading
    (I think )

    [edit] ok so maybe I was wrong
    Last edited by sand_man; 09-07-2005 at 06:22 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,052
    I would use variable arguments.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    This seems very static, I don't want to change the exsiting code, I just want to add functionality to an already exsiting function.
    Isn't there anything more similar to default values in c++?

    I don't want to pass any values apart from the ones that are to be treated.
    Like i said.....

    logvt->append(1)
    logvt->append(1,2)

    No values concerning the number of args or anything like that....

    Can anyone tell me if this is possible in c?

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    No, it's not possible in C. The closest approximation to that is variable arguments, but you need a parameter to specify the number of arguments passed.

    If you were feeling really perverted, you could write a custom preprocessor for your C program which mangled your append calls into two separate functions, effectively emulating overloading.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Try this,

    Code:
    #include <stdio.h>
    
    typedef int (*func_ptr)();
    
    int add (int a, int b)
    {
      return (a+b);
    }
    
    int sqr (int a)
    {
      return (a*a);
    }
    
    int main()
    {
      func_ptr fp;
      
      fp = add;
     
      printf("Sum of 2 and 3 is %d\n",fp(2,3));
    
      fp = sqr;
      
      printf("square of 2 is %d\n",fp(2));
    
      return 0;  
    }
    Program Output

    [root@olinux1 tools]# ./ptrfunc
    Sum of 2 and 3 is 5
    square of 2 is 4
    [root@olinux1 tools]#

  12. #12
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    nkhambal, that is an abomination :P

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    Not really shure what you wanted to achieve by that nkhambal!!!

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    oops sorry!!! I think, I lost the objective in OP. My apologies. I though the OP wanted to know how to use a single function pointer to call multiple functions which accepts different number of arguments.
    Example is still valid , although not related to the OP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  3. Replies: 11
    Last Post: 08-28-2008, 04:10 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Replies: 2
    Last Post: 11-22-2001, 11:22 AM