|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Re: Eval() in C?
I basically want to be able to store a string in a struct and then be able to call that method when "the time is right." It's almost like Flash's event based structure, so I can't really have separate if statements for every string.
EDIT: mluckham, that'll probably work, thanks. |
|
#2
|
|||||
|
|||||
|
Re: Eval() in C?
Quote:
-Danny |
|
#3
|
||||
|
||||
|
Re: Eval() in C?
Pretty advanced (function pointers) but they are certainly useful as 'action routine pointers' in a state-action table (look up "Finite State Machine") provided the argument list to all the routines called is identical.
|
|
#4
|
||||
|
||||
|
Re: Eval() in C?
Quote:
The following code compiles with ANSI C. While running it waits for individual characters. If the character matches the command character 'cmd' then it de-references the function pointer, and calls the function. It passes no arguments onto the called function. (Hint: that's an exercise for you). Code:
#include <stdlib.h>
#include <stdio.h>
// Four command functions. They must have identical arguments.
void go_left(void) { printf("Going Left\n"); }
void go_right(void) { printf("Going Right\n"); }
void stop(void) { printf("Stopped\n"); }
void quit(void) { printf("Bye\n"); exit(0); }
// An array of a two value structures.
struct {
char cmd; // The character
void (*function)(void); // a pointer to a function.
} commands[] = {
{'l', &go_left}, // put the address of the function
{'r', &go_right}, // into the function pointer
{'s', &stop},
{'q', &quit},
{0, 0} // marks the end of the array.
};
int
main(void) {
char c; int i;
while (1) {
c = getchar(); // not available on FRC
i = 0;
while (commands[i].cmd) {
if (commands[i].cmd == c) {
// dereference the pointer and call the function.
// note the bracketing
(*(commands[i].function))();
}
i++;
}
}
}
You do not have a "getchar()" function in the FRC code instead you'll have to read a character from one of the serial ports. The above code is not pretty, badly commented, terse, and follows an arcane indenting style (I'm old). You should use 'typedef' to define the structure instead of using it inline like this. The structure is initialized after the command functions have been declared. If the command functions are in another file then you'll have to use 'extern' declarations of the functions before initializing the array. A sample run: Code:
$ cc ex1.c $ ./a.out l Going Left r Going Right q Bye $ |
|
#5
|
|||
|
|||
|
Re: Eval() in C?
The only limitation is that your functions must take the same datatype. If you need them to take different data types you will need to make them all take a single void pointer to a block of memory then the function will need to reconstruct the data itself. This is probably too complicated and you probably won't need it. just throwing it out there if the need comes up
|
|
#6
|
||||
|
||||
|
Re: Eval() in C?
Quote:
it is possible to create an eval function, using some derivative of a reverse polish notation format. however, for the purposes of robotics, it probably wont be worth it. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Summer camp program eval. | Andrew Schuetze | General Forum | 3 | 12-06-2006 22:16 |