Thread: Can you make an array of commands?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Can you make an array of commands?

    Hello,

    I hope you are having a good day and thank you for your time.

    Is it possible to make an array of commands such that if I want to go to a function with a certain input, I can use the input as an offset which will access a certain command and then return?
    (some of this is pseudo code, so I left out the ;'s)
    Code:
    array1[7] = {command1,command2,command3,...,command7}
    int main(void)
    {
    //do some stuff
    get input from keyboard
    n = keyboard input //pseudo code
    array1[n]  //Can I do this?
    }
    Can I just write an array of commands like this?:
    Code:
    cmdarray1[10] = {PlaySnd(6), SysBeep(200), SendString("Hello"),....., DelaySecs(4)}
    
    and access it like this?:
    i = keyboard input //pseudo code
    cmdarray1[i];
    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can make an array of function pointers, but not of function calls (with the parentheses already filled in).

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    [SIZE=1][SIZE=1]
    Quote Originally Posted by tabstop View Post
    You can make an array of function pointers, but not of function calls (with the parentheses already filled in).
    So I looked at the "Array of Function pointers" stuff and it looks really helpful.

    So I made my array:
    Code:
    int (*FunArray1[8]); 
    	FunArray1[0]=playPT1;
    	FunArray1[1]=playPT2;
    	FunArray1[2]=playPT3;
    	FunArray1[3]=playPT4;
    	FunArray1[4]=playPT5;
    	FunArray1[5]=playPT6;
    	FunArray1[6]=playPT7;
    	FunArray1[7]=playPT8;
    and in my program, I do this:
    Code:
    if((the user did something))
    {
    	while(funIndex<=8) //don't want to go above the index
    	{
    	*FunArray1[funIndex];   //assume funIndex=0
    	}
    }
    void playPT1(void)
    {
    	
    	PlaySnd();//this works fine, I just took out the variables for simplicity
            funIndex++;
    	return funIndex;
    }
    Error message:
    type mismatch, expected 'int (*)(int)', found 'void (*)(void)'

    Thank you for your help!!!

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    All I really want to do is go to the function and do something then return nothing.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    A 2d array could be used to store the arguments.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Or you could make life a whole lot simpler and just use a switch() statement to call functions based on your keyboard input.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Or you could make life a whole lot simpler and just use a switch() statement to call functions based on your keyboard input.
    There you go trying to make things simple again Tater.

    As for the OP, Tater's point really is the simpliest and best solution for what it appears you want to do. However, if you want to go the function pointer route, for academia I suppose, here is a quick example:
    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");
    }
    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.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Quote Originally Posted by AndrewHunter View Post
    There you go trying to make things simple again Tater.

    As for the OP, Tater's point really is the simpliest and best solution for what it appears you want to do. However, if you want to go the function pointer route, for academia I suppose, here is a quick example:
    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");
    }
    Are you sure that
    Code:
    myFunctionTable FunctionTable[]={{"Hello", &myHello}, {"GoodBye", &myGoodBye}};
    is correct syntax?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ☼Aulos View Post
    Are you sure that
    Code:
    myFunctionTable FunctionTable[]={{"Hello", &myHello}, {"GoodBye", &myGoodBye}};
    is correct syntax?
    Is there a reason you believe it isn't? (For example, if you tried it and the compiler complained, then that would be a reason that presumably you would tell us about.)

    In fact, it is almost but not quite right syntax, as the word "struct" needs to be right before it.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tabstop View Post
    In fact, it is almost but not quite right syntax, as the word "struct" needs to be right before it.
    D*** C++ compiler foils me again.
    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.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you'll have to decide what type of things you want: do you want functions that take an int and return an int (which is "int (*)(int)"), or that take no arguments and return nothing (which is "void (*)(void)")? They aren't compatible with each other.

    You should also get at least a warning for trying to return funIndex from a void function.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Just use switch statements, they are easier and quicker.
    Code:
    void foo(void);
    
    int main(void){
    
        int answer;
        printf("Select:");
        scanf("%d", &answer);
      
        switch (answer){
             case 1:
                  foo();
                  break;
             .....
        }
    ....
    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.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's what your declaration of FunArray looks like to the compiler. Note, I added the parentheses in purple that you seem to have forgotten, or that the compiler filled in for you.
    Code:
    int (*FunArray1[8])(int);
    FunArray is an array of 8 pointers to functions that take an int and return an int.

    Here is the declaration of playPT1 you try to assign to your array:
    Code:
    void playPT1(void)
    playPT1 is a function that takes nothing and returns nothing.

    Notice how the purple and red parts don't match up between the two? For proper assignment, function pointers have to match parameter count and types as well as return type. You either need to make FunArray void-void:
    Code:
    void (*FunArray1[8])(void);
    or make your playPT functions int-int:
    Code:
    int playPT1(int x)

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    So when I do this:
    Code:
    *FunArray1[funIndex];
    I am passing a number(funIndex) to (*FunArray1[8]) which means that its input must be int.

    so:
    Code:
    int (*FunArray1[8])(int funIndex);
    So the number passed to the *FunArray1 is the index, so that's why I use "int" and doesn't the *FunArray1 return an address? So shouldn't it's output be an int? (like above)
    But, since the *FunArray1 is going to the index of the function, I don't see why the function, void playPT1(void) should have any int's in either the input or the output of the function.

    However, when I type in "return;" in the function void playPT1(void) Where is it returning? Is it returning to the main program or the *FunArray1?

    And by the way, this is a voice controlled program running on a microcontroller. So, the person is not typing on a keyboard.
    Last edited by ☼Aulos; 07-21-2011 at 01:54 PM.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Quote Originally Posted by anduril462 View Post
    Here's what your declaration of FunArray looks like to the compiler. Note, I added the parentheses in purple that you seem to have forgotten, or that the compiler filled in for you.
    Code:
    int (*FunArray1[8])(int);
    FunArray is an array of 8 pointers to functions that take an int and return an int.

    Here is the declaration of playPT1 you try to assign to your array:
    Code:
    void playPT1(void)
    playPT1 is a function that takes nothing and returns nothing.

    Notice how the purple and red parts don't match up between the two? For proper assignment, function pointers have to match parameter count and types as well as return type. You either need to make FunArray void-void:
    Code:
    void (*FunArray1[8])(void);
    or make your playPT functions int-int:
    Code:
    int playPT1(int x)
    Does it matter whether the functions that the array is pointing to return or take values? I wouldn't think so, so I made the the functions void-void.

    What does the input to the functions have to do with its index on the array?

    I thought "int (*FunArray1[8])(int);" took a value (funIndex) and returns an address, the the program goes to the function and when I type in "return;", it goes back to the main program? Am I wrong? ( I realize I probably am wrong about everything I've said).


    Can I use the int "int (*FunArray1[8])(int funIndex);" (its declaration) like this:

    *FunArray1[funIndex];

    to go to the function I want, then, in the function put "return;" to go back to the main program?

    So I guess this is where I am confused especially, why do I have to declare the array like this:
    int (*FunArray1[8])(int funIndex);

    When I am passing a parameter into the array (funIndex) within the []'s?

    Could I declare the function like this:
    int (*FunArray1[int funIndex = 8]; since the []'s is where I am passing the index right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make an array of classes in c#
    By alireza in forum C# Programming
    Replies: 8
    Last Post: 11-16-2010, 05:16 PM
  2. Make an array of arrays
    By JOCAAN in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 12:18 PM
  3. How do I make a *HUGE* array in C++?
    By rooster in forum C++ Programming
    Replies: 10
    Last Post: 02-27-2006, 08:05 PM
  4. how to make an array of strings
    By rozner in forum C Programming
    Replies: 2
    Last Post: 03-24-2003, 01:18 PM
  5. how to make a poiner array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:12 PM

Tags for this Thread