Thread: Allocating functions to an array of function pointers inside a struct

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Allocating functions to an array of function pointers inside a struct

    Suppose I have 10 functions.
    f0,f1.....f9 (all void x(void))
    and a
    void (*func)(void)[10] ; //this is inside a struct (or a c++ class )

    and there is another function
    void foo(object,int); // or object.foo(int) in c++

    How would I specify(with the minimum amount of code involved) that
    foo(object,n) should call *(func[n])() which is, in turn synonymous to a call to fn()
    ?

    [Do tell if a c++ based solution also works ]
    Last edited by manasij7479; 07-03-2011 at 01:14 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends on where you're looking for the code. You've got to manually assign all the slots in your func array (although if you really did call your functions f1 through f9 you can do some fancy pre-processor trickery with ##). The translation part would appear to be trivial, if it wasn't for that "inside a struct" thing:
    Code:
    void foo(X object, int n) {
        (func[n])();
    }
    Since you put your array of functions inside the struct, then hopefully that's the object you're passing in, which means we now have
    Code:
    void foo(X object, int n) {
        (object.func[n])();
    }

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    although if you really did call your functions f1 through f9 you can do some fancy pre-processor trickery with ##
    Unfortunately not, and the functions have rather colourful names and are more then 10 in number..


    You've got to manually assign all the slots in your func array
    Even when I have to do it manually, where do I actually assign them? I can't just put a func[0] = &f0 inside the struct...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think you may be mistaken on what a struct is. A struct is not a thing. A struct is what a thing would look like, if it were to exist. You have to create an object to actually have something. So:
    Code:
    struct hey_a_struct {
        int numbers[10];
    }
    
    /* at this point I have nothing at all */
    int main(void) {
    struct hey_a_struct object; /*now I have something */
    for (int i = 0; i < 10; i++) {
        object.numbers[i] = i*i;
    }

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    That is why I said I can't put an assignment inside it..

    btw....your post gave me another idea...(If allowed by C)
    When I require all the function pointers in the func array regardless of the object, to point to the same range of functions, can I make the array static..and then assign the functions to them manually with a "type:: Object = value;" as I've commonly seen/done in C++ ?

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    A simple example is this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void  myHello(void);
    void  myGoodBye(void);
    
    int main(void){
    
    	//create framework for function table
    	struct myFunctionTable{
    		char *myName;
    		void (*myFunction)(void);
    	};
    
    	//make function table, aka array
    	myFunctionTable FunctionTable[]={{"Hello", &myHello}, {"GoodBye", &myGoodBye}};
    
    	char myAnswer[10];
    
    
    	printf("Which function to run?");
    	scanf("%s", myAnswer);
    
    	//find and run function
    	for(int i = 0; i < (sizeof(FunctionTable) / sizeof(myFunctionTable));i++){
    		if(strcmp(FunctionTable[i].myName,myAnswer)==0){
    			FunctionTable[i].myFunction ();
    			break;
    		}
    	}
    	getchar();
    	getchar();
    	return (0);
    }
    //Implement functions
    void myHello(void){
    	printf("Hello World\n");
    }
    void myGoodBye(void){
    	printf("GoodBye World\n");
    }
    Also Salem posted a C++ version a couple of days ago. If you look around on the net you will be able to find many solutions to this which include hash and jump table implementations.

    EDIT: In fact Salem gave you this answer a couple of days ago on the C++ board.
    Last edited by AndrewHunter; 07-03-2011 at 07:58 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Thank you

    EDIT: In fact Salem gave you this answer a couple of days ago on the C++ board.
    Those were different questions. Please look a little more closely before jumping to such conclusions. Just because they involved function pointers, doesn't mean they are congruent.
    Last edited by manasij7479; 07-03-2011 at 09:16 PM.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by manasij7479 View Post
    .....
    [Do tell if a c++ based solution also works ]
    No offense meant, just wasn't sure if you saw it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by manasij7479 View Post
    That is why I said I can't put an assignment inside it..
    I think you've missed my point. Of course you can assign bits of a struct; if you aren't trying to make a new datatype and expect to have bunch of these structs running around, then a struct is not what you're looking for. Here (just based on what you've posted in the last couple threads) a struct is silly and an array is perfect. So make an array.

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I think you've missed my point. Of course you can assign bits of a struct; if you aren't trying to make a new datatype and expect to have bunch of these structs running around, then a struct is not what you're looking for. Here (just based on what you've posted in the last couple threads) a struct is silly and an array is perfect. So make an array.
    err...You slightly misunderstood my answer...
    But your point about using only an array is a good one when using only C. (don't scoff at this!)
    Then, I can call those functions with the first argument taking an object.

    I figured out that I'd use an array of pointers to static member functions in C++ for that.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Erm, static member functions don't have a special pointer type. They convert to regular function pointers.

    tabstop is spot on.
    Last edited by King Mir; 07-05-2011 at 07:18 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Ok, then..
    maybe I was thinking in a manner too convoluted.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    This is not hard. Glad you asked.

    I like questions like this. I had to think about it for a while before I realised that I do this all the time. Of course, in C I don't put the function right in the structure, but I can definitely point to it.

    I would write the function like this:
    Code:
    void foo(int i) {
       void(*foo[]) = {
          foo1,
          foo2, 
          foo3,
          foo4,
          foo5
          /* etc */
       };
       foo[i]();
    }
    Remember, all of the foo's in the list are "void foo1( ); void foo2();" etc.

    When you call foo(3), it will then call foo3. At least this seems to be what you are asking for in your question. If it is not what you asked for (and, somehow, I don't think it is) let me know! Do pay close attention to the syntax, it is very important!

    phlsphr

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Ok, then..
    maybe I was thinking in a manner too convoluted.
    An array solution can be quite simple... All you need is the name of the function followed by a pointer to the function... leaving you with code that basically finds the string in array[i][0] then executes the pointer in array[i][1].

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    I have to make one adjustment to my last post. Calling foo(0) will call foo1, calling foo(1) will call foo2, etc. Sorry about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array inside struct
    By tat in forum C Programming
    Replies: 15
    Last Post: 11-13-2007, 12:36 PM
  2. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM
  3. allocating array of pointers
    By l2u in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2006, 10:11 AM
  4. Dynamicly allocating an array of pointers
    By kzar in forum C Programming
    Replies: 2
    Last Post: 05-05-2005, 04:50 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM