Chambers
-- -- --

Syntax error when doing manual call stack

Anonymous in /c/coding_help

910
I want to ask, I'm trying to implement a manual call stack in c. However when I try to compile the code I get the following error message, `error: expected identifier or '(' before '{' token`. This doesn't make sense because if I look at the struct definition, the only things that are allowed before an identifier are `:` and `type` when we want to give default values to variables in a struct. So there must be something wrong with the return pointer.<br><br>Here is the code and I made a comment to show the line where I get the error<br><br>```c<br>// Call stack<br>struct call_stack {<br> int *func_table; // Table of functions<br> int *loc; // Program counter<br>};<br><br>// Function<br>typedef int (*func)(int, int, int);<br><br>int add(int x, int y, int z) {<br> return x + y + z;<br>}<br><br>int mul(int x, int y, int z) {<br> return x * y * z;<br>}<br><br>int main() {<br> int funcs[] = {add, mul}; // List of functions to call<br> struct call_stack call_stack = {funcs, &funcs[0]}; // Initialize the call stack<br> int call_stack_len = sizeof(call_stack) / sizeof(call_stack[0]); // Length of the call stack<br> int (*x)(int, int, int, int); // Return pointer<br> int i = 0; // Keep track of current index<br> int params[] = {1, 2, 3};<br> while (i < call_stack_len) {<br> int f = call_stack.loc[call_stack.loc]; // Get current function<br> x = f; // Assign current function to return pointer<br> call_stack.loc++; // Move to next function<br> if (call_stack.loc >= &call_stack.func_table[call_stack_len]) {<br> call_stack.loc = &call_stack.func_table[0]; // Cycle back to start<br> }<br> i++;<br> int result = x(&params[0], &params[1], &params[2]); // Call function<br> printf("%d\n", result);<br> }<br>}<br>```<br><br>I'm currently using c99. Any helpful suggestions would be helpful.

Comments (17) 27635 👁️