PDA

View Full Version : (uninteresting) parsing and virtual functions



ggs
03-19-2002, 04:50 PM
So far work on the source file lexer has been proceeding nicely... Most of C is fairly straightfoward to parse. So I'm writing the name mangler, I'm just about done it... when I realize I've forgotten about function pointers. I'm not even sure I remember the correct syntax for using a function pointer, or an array of functions pointer... How should I test for a declaration of a function pointer? Where exactly do they go? You have variables, you have functions, you have pointers.. and you have function pointers. It's like a new category of thingies that doesn't exactly fit under any of the other category, and cannot be treated under any of the other rules. Damn. More work :(

DavidP
03-19-2002, 06:07 PM
Function pointer:

typedef void (fnptr*)(void);

Array of functions, using that function pointer:

//declare and define functions

void f1 ( void ) {
printf("Hello from 1");
}

void f2 ( void ) {
printf("Hello from 2");
}

void f3 (void) {
cout << "Hello from 3";
}

void f4 (void) {
cout << "Hello from 4";
}

void f5 (void) {
cout << "Hello from 5";
}

/* initialise the array of fn pointers */
/* a pointer to a fn is just its name */

fnptr array[5] = {
f1, f2, f3, f4, f5
};

Main function using array:

int main (void)
{
int i;
for ( i = 0 ; i < 5 ; i++ ) {
array[i]();
}
getch();
return 0;
}