Thread: Can you make an array of commands?

  1. #16
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You appear to be new at programming, and as such function pointers may not be the way to go with what you want to do. I posted 2 examples -- see above. The first one involves a lookup table (the one you are commenting on now) and the second one (the one I was referring to in my post) does not and is a more straighforward implementation of function pointers. I would suggest looking at that one.

    Some things to note, I took the time to post 2 complete working programs for a reason - so you can copy them into your IDE and have a complete program to play with so you can start to learn how it works. By your questions it appears that you aren't doing this and thus I must have been just wasting my time.

    Do me a favor, create a complete example of what you want to do. An actual program (use psuedocode where you don't know how to implement actual code). Your fun functions do not need to be fully implemented here, just give us the main body of the program. This way we can actually see what you want and then provide a way forward for you.

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

  2. #17
    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?

  3. #18
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Did you try to run the program? Does it work? Do you know what an array is or how you can implement them in C? Did you even bother to look at my other posts as I told you to? Am I the one asking for help?
    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.

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

  5. #20
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    I apologize. My last post was put out before I saw yours starting with "You appear to be".

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

  7. #22
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    All,

    Sorry I wasn't more clear. When I was asking questions, I was messing around with the code at the same time.

    Well I tried the look up table method of function pointers and can't seem to get it to work although I though I knew how it did. For some reason, my compiler wants a ';' in front of
    Code:
    struct	MyFunctionTable FunctionTable[]={{"Hello", &myHello}, {"GoodBye", &myGoodBye}};
    I did that and it said "illegal initialization".

    I have a code that reads sections of text to the user.

    Instead of just reading the sentences one after another, I would like the program to have the ability of sampling the microphone in between sections (which works fine) for "fast forward" and "repeat". The code can recognize "continue","stand by", "repeat" and "fast forward" (on the microcontroller).

    I would like a section of the program with a variable "DeltaIndex" which will change values and then read the corresponding section of the text, test for "repeat" and "fast forward" again, and do it again. Here is what the code looks like:


    Code:
    DeltaIndex=0; // Start DeltaIndex=0 at the beginning of every text reading
    if(the user said read a certain text)
    {
        while(DeltaIndex<=8)  //Don't want to go past the number of sections
        {
            go to the function that reads the current section based on the index, test the mic for a word ("continue","stand by", "repeat" and "fast forward") then do that and come back here and re-evaluate the while statement then go to the function that reads the current section based on the index.
         }
    }
    I am currently trying to implement the suggestions above. I emphatically appreciate your help.

  8. #23
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    So it looks like my program never actually goes to the function pointed to by DeltaIndex in the array "FunArray1":

    Code:
    
    if((the user said "watermelon"))
    {
    	while(DeltaIndex<=8)
    	{
    		_SendString232((long)"\n\r you should now go to playPT1\r\n");
    		FunArray1[DeltaIndex];
    	}
    }
    It goes in an infinite loop (which makes sense since I set DeltaIndex to 0)and prints "you should now go to playPT1" an infinite number of times and never goes into FunArray1, looks at the element with index of "DeltaIndex" and go to the function pointed to by that address, it just "skips over" FunArray1[DeltaIndex] and drops out of the program and restarts.

    Code:
    void (*FunArray1[8]) (void);
    
    	FunArray1[0]=&playPT1;
    	FunArray1[1]=&playPT2;
    	FunArray1[2]=&playPT3;
    	FunArray1[3]=&playPT4;
    	FunArray1[4]=&playPT5;
    	FunArray1[5]=&playPT6;
    	FunArray1[6]=&playPT7;
    	FunArray1[7]=&playPT8;
    .
    .
    .
    .
    
    void playPT1(void)
    {
    	_PlaySnd(256); // this works
            DeltaIndex++;
    	return;
    }
    Why is it just skipping over it? How do I make the program go there? I was hoping it wouldn't go in an infinite loop because I increment DeltaIndex in the playPT1 function.

    I emphatically thank you for your help.
    Last edited by ☼Aulos; 07-22-2011 at 04:51 PM.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	while(DeltaIndex<=8)
    	{
    		_SendString232((long)"\n\r you should now go to playPT1\r\n");
    		FunArray1[DeltaIndex];
    	}
    DeltaIndex[ 8 ] is out of bounds.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It is because you are not actually calling the function. Look at your code:
    Code:
    if((the user said "watermelon"))
    {
    	while(DeltaIndex<=8)
    	{
    		_SendString232((long)"\n\r you should now go to playPT1\r\n");
    		FunArray1[DeltaIndex];
    	}
    }
    Does anywhere in there look like a function call? All the compiler is seeing with this:
    Code:
    FunArray1[DeltaIndex];
    is that you are listing some random memory address. In order to actually have the compiler call the function you would need something like:
    Code:
    if((the user said "watermelon"))
    {
    	while(DeltaIndex<8)
    	{
    		_SendString232((long)"\n\r you should now go to playPT1\r\n");
    		FunArray1[DeltaIndex]();
    	}
    }
    Note the ' () '. Now the compiler knows that you want to execute a function call. Also note I adjusted your while - loop like quzah pointed out so you wouldn't go out of bounds.
    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. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course you are still trying to access FunIndex1[ 8 ] ...


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #27
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by ☼Aulos View Post

    I have a code that reads sections of text to the user.

    Instead of just reading the sentences one after another, I would like the program to have the ability of sampling the microphone in between sections (which works fine) for "fast forward" and "repeat". The code can recognize "continue","stand by", "repeat" and "fast forward" (on the microcontroller).

    I would like a section of the program with a variable "DeltaIndex" which will change values and then read the corresponding section of the text, test for "repeat" and "fast forward" again, and do it again. Here is what the code looks like:
    1. How does this program get the text to read to the user?
    2. Where is this text stored during program execution?
    3. How did you implement "reading the text to the user" (I don't need code for txt -> audio, just where and how called)
    4. How do you keep track of where the program currently is in the text?

    I am asking these questions because it is starting to appear to me that something is amiss with your program design.
    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. #28
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    Of course you are still trying to access FunIndex1[ 8 ] ...
    Quzah.
    Who is?
    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.

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    Who is?
    People in the real world? I thought I saw the = in there still. I guess I got the top code and the bottom section confused.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    People in the real world? I thought I saw the = in there still. I guess I got the top code and the bottom section confused.
    Quzah.
    LOL....nope, and I even gave you credit for it. Well I guess we will both have to wait until we are out of highschool to find out what happens in the real world.
    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.

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