Here's some sample code I just wrote. Take a look at it and you'll see what I'm trying to explain. Does it do what you want it to do?

[edit] Wait, I think my algorithm is flawed . . . [/edit]Code:#include <stdio.h> struct data_t { int n; int (*func)(int x, int n); const char *name; }; int func_add(int x, int n) { return x + n; } int func_multiply(int x, int n) { return x * n; } int func_subtract(int x, int n) { return x - n; } void permutations(int x, struct data_t *data, size_t n); int main() { struct data_t data[] = { {3, func_add, "+"}, {3, func_multiply, "*"}, {1, func_subtract, "-"} }; permutations(17, data, sizeof(data)/sizeof(*data)); getchar(); return 0; } void permutations(int x, struct data_t *data, size_t n) { if(!n) return; permutations(x, data + 1, n - 1); printf("%d %s %d = ", x, data[0].name, data[0].n); x = (*data[0].func)(x, data[0].n); printf("%d [%d]\n", x, 3-n); permutations(x, data + 1, n - 1); putchar('\n'); }

[edit=2] This simpler program works better:

[/edit]Code:#include <stdio.h> void permutations(int *data, size_t n, const char *prev); int main() { int data[] = {1, 2, 3}; permutations(data, sizeof(data)/sizeof(*data), "combination:"); getchar(); return 0; } void permutations(int *data, size_t n, const char *prev) { char stack[100]; if(!n) return; permutations(data + 1, n - 1, prev); sprintf(stack, "%s %d", prev, *data); printf("%s %d\n", prev, *data); permutations(data + 1, n - 1, stack); }

[edit=3] There you go, this one's much easier to understand:

Have you ever programmed a solution to the Towers of Hanoi? It's quite similar to this. [/edit]Code:#include <stdio.h> struct data_t { int n; int (*func)(int x, int n); const char *name; }; int func_add(int x, int n) { return x + n; } int func_multiply(int x, int n) { return x * n; } int func_subtract(int x, int n) { return x - n; } void permutations(int x, struct data_t *data, size_t n, const char *prev); int main() { struct data_t data[] = { {3, func_add, "+"}, {3, func_multiply, "*"}, {1, func_subtract, "-"} }; permutations(17, data, sizeof(data)/sizeof(*data), "combination:"); getchar(); return 0; } void permutations(int x, struct data_t *data, size_t n, const char *prev) { char stack[BUFSIZ]; int t; if(!n) return; permutations(x, data + 1, n - 1, prev); t = (*data[0].func)(x, data[0].n); sprintf(stack, "%s [%d %s %d = %d]", prev, x, data[0].name, data[0].n, t); puts(stack); permutations(t, data + 1, n - 1, stack); }

[edit=4] Here's the Wikipedia entry for "Towers of Hanoi": http://en.wikipedia.org/wiki/Tower_of_Hanoi

It includes a recursive solution written in C. [/edit]