Here's one approach.
Code:
#include <stdio.h>
// Probably in separate source files
int main1(int argc, char *argv[] ) {
printf("This is example 1\n");
return 0;
}
int main2(int argc, char *argv[] ) {
printf("This is example 1\n");
return 0;
}
// In your new menu main
extern int main1(int argc, char *argv[]);
extern int main2(int argc, char *argv[]);
struct {
const char *title;
int (*fn)(int, char*[]);
} menu[] = {
{ "Example 1", main1 },
{ "Example 2", main2 },
};
int main (int argc, char *argv[]) {
for ( size_t i = 0 ; i < sizeof(menu)/sizeof(*menu) ; i++ ) {
printf("%zd %s\n", i, menu[i].title );
}
printf("Choose > ");
int choice;
scanf("%d",&choice);
return menu[choice].fn(argc, argv);
}
The function pointer on line 21 is a whole rabbit hole you might want to avoid going down for the moment.
So long as all your mainx functions have the same prototype, you can just go with it.
Note that if you're calling any C++ code at all, then your actual main() function needs to be compiled in a .cpp file.