Like Tree5Likes

Can you make an array of commands?

This is a discussion on Can you make an array of commands? within the C Programming forums, part of the General Programming Boards category; Hello, I hope you are having a good day and thank you for your time. Is it possible to make ...

  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,185
    You can make an array of function pointers, but not of function calls (with the parentheses already filled in).

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    A 2d array could be used to store the arguments.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    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.
    AndrewHunter likes this.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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.

  6. #6
    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!!!

  7. #7
    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.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,304
    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)

  11. #11
    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.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Here, maybe this example will be a little clearier to you since it doesn't use a lookup table and has functions which take a parameter of type int and return nothing.

    Note: Regardless of how your program is getting input, I am sure there is a simplier way that doesn't involve function pointers.

    Code:
    #include <stdio.h>
    
    //declare functions
    void foo(int);
    void foo1(int);
    void foo2(int);
    
    int main(void){
    	
    	//create function array for functions which return nothing (void) and accept 1
    	// int parameter
    	void(*myFunction[3])(int);
    	
    	//now we need to assign each of those pointers to an actual function so they know
    	//where to point to
    
    	myFunction[0]= &foo;
    	myFunction[1] = &foo1;
    	myFunction[2] = &foo2;
    
    	//now we are going to call the functions in our array and pass an int
    	//parameter to them
    	for (int i = 0; i < 3; i++){
    		myFunction[i](i + 1);
    	}
    	
    	getchar();
    	return(0);
    }
    
    //implement our functions
    
    void foo(int a){
    	printf("foo: %d\n", a);
    }
    void foo1(int b){
    	printf("foo1: %d\n", b);
    }
    void foo2(int c){
    	printf("foo2: %d\n", c);
    }
    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
    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?

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Look at the example I posted. It shows you how to do this.
    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.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Quote Originally Posted by AndrewHunter View Post
    Look at the example I posted. It shows you how to do this.
    your code:
    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");
    }
    Why is the array FunctionTable[] not have a specified # of elements?
    So basically, the user's input gets put into myAnswer. (why is myAnswer an array of 8 bit elements? Isn't it a string?)
    Then, you try to find myAnswer in the array FunctionTable and then run the corresponding function.

    So you basically do a for loop for as long as the array Function Table is. Why do you divide FunctionTable by myFunctionTable?

    Is myfunctionTable defined as the stuff in the "" 's ("Hello", and "GoodBye") and FunctionTable is defined as the function addresses (stuff with the &'s)?
    Or is the "char *myName defined as the stuff in the "" 's ("Hello", and "GoodBye") and "void (*myFunction)(void)" defined as stuff with the addresses (&'s)?

    The second possibility makes more sense for the "if" statement.

    Does "if(strcmp(FunctionTable[i].myName,myAnswer)==0)" mean that "if "myAnswer" is the same as "Hello" or "GoodBye" (myName) then go to the function now associated with the current i value?

    So by assigning the struct (myFunctionTable) infront of the array, FunctionTable, you are able to access both parts of each element in the array, giving you the ability of simultaneously comparing myAnswer with "Hello" and "GoodBye" and then also having the address of the function? Is that right?

Page 1 of 3 123 LastLast
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, 04:16 PM
  2. Make an array of arrays
    By JOCAAN in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 11:18 AM
  3. How do I make a *HUGE* array in C++?
    By rooster in forum C++ Programming
    Replies: 10
    Last Post: 02-27-2006, 07:05 PM
  4. how to make an array of strings
    By rozner in forum C Programming
    Replies: 2
    Last Post: 03-24-2003, 12:18 PM
  5. how to make a poiner array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 08:12 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21