GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Feb-2005, 08:09
mikhail mikhail is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Dublin, Ireland
Posts: 73
mikhail is on a distinguished road

How to pass a function name as an argument to another function


I found and adapted the following suggestion from elsewhere, but it crashes my compiler. I'm using Interactive C (www.handyboard.com), so that may be the only problem, but does anyone know if I'm using this wrong, or if there are any other tricks to do this?

CPP / C++ / C Code:
int main()
{
    void *addr=on;
    
    rrstart_process(*addr);
    sleep(3.0);
    ao();      
}

int rrstart_process(void *addr)
{
    void(*f)();
    f = addr;
    
    start_process(f());
}

void on()
{
    motor(1, 100);
}


p.s. My project partner insists I use this smilie: Sorry.
  #2  
Old 04-Feb-2005, 08:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by mikhail
I found and adapted the following suggestion from elsewhere, but it crashes my compiler. I'm using Interactive C (www.handyboard.com), so that may be the only problem, but does anyone know if I'm using this wrong, or if there are any other tricks to do this?

Since you didn't show all of your code, I can't really suggest corrections.

However...
Here is an example that is standard C (also standard C++) If your compiler doesn't like this (or it doesn't work), ask for your money back.

You should be able to use this to see how to change your program. If you don't understand how I declared and used the pointer-to-function, ask for additional explanations.


CPP / C++ / C Code:
/* 
 * Illustration of declaration and use of pointer to function 
 */

#include <stdio.h>

int main()
{
  void func1(int);
  void func2(int);

  /* The following is the prototype of a function that has two arguments.
   * The first argument of dispatch() pointer to a function.
   * The second argument of dispatch() is an int.
   * 
   * The first argument of dispatch() points to a function that is of 
   * type void, and has one argument that is an int
   */
  void dispatch(void(*)(int), int);

    /* The following says that addr is a pointer to a function.
     *
     * The function that it will point to:
     *   has type void, and has a single argument of type int
     */
    void (*addr)(int);

    int x;
    
    /*
     * First, call the functions directly, using pointer to func
     */
    x = 19;
    addr = func1;

    printf("\n\n   Calling addr(%d), with addr = func1\n", x);
    addr(x);

    x = 41;
    addr = func2;

    printf("\n   Calling addr(%d), with addr = func2\n", x);
    addr(x);

    /* 
     * Now, show how to use the dispatch() function with different arguments
     */
    x = 19;
    addr = func1;

    printf("\n\n   Calling dispatch(addr, %d) with addr = func1\n", x);
    dispatch(addr, x);

    x = 41;
    addr = func2;

    printf("\n\n   Calling dispatch(addr, %d) with addr = func2\n", x);
    dispatch(addr, x);

    return 0;
}

void func1(int n)
{
  printf("       This is func1(): n = %d\n", n);
}

void func2(int n)
{
  printf("       This is func2(): n = %d\n", n);
}

void dispatch(void(*func)(int), int arg)
{
  printf("     In dispatch(), arg = %d\n", arg);
  (*func)(arg);
}


Regards,

Dave
  #3  
Old 04-Feb-2005, 10:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by mikhail
I found and adapted the following suggestion from elsewhere, but it crashes my compiler. I'm using Interactive C (www.handyboard.com), so that may be the only problem,


I just now looked at your link. My previous response in standard C may, indeed, not work for this micro version subset of C (The wisecrack about getting your money back was uncalled for.) I have no way of testing your code (or mine) since I don't have a handyboard to use for investigation.
Anyhow, the notation that I used in my example is standard C. You can try it and see what happens.

Good Luck!

Regards,

Dave

p.s.
My attitude towards C running on 8-bit 1980s-vintage microcontrollers is kind of like my opinion of dancing bears: I'm impressed not by how well they dance, but that they dance at all.

(If I knew who first used the "dancing bear" metaphor, I would give them credit.)

D.
Last edited by davekw7x : 04-Feb-2005 at 10:41.
  #4  
Old 17-Jun-2008, 21:09
cgrob cgrob is offline
New Member
 
Join Date: Jun 2008
Posts: 1
cgrob is on a distinguished road

Re: How to pass a function name as an argument to another function


Thanks to Dave for taking the time to post that!

I was stuck and frustrated getting a complex bit of code to run but your clearly commented example did the trick.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 08:48
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 13:02
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:50.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.