Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function only knowing parameter types at runtime in C?

Tags:

c

Let's say I have a function:

int foo (int A, char B){...}

One of the features I want to implement is the capability for the user to call any function on the application through the Linux terminal. So as an input for the software, in the terminal they type something like:

foo 2 'a'

Then my application parses that, and using the symbol tables it is able to find the address for foo(), as well as the type for all its parameters.

However, I'm not sure how I would pass the parameters to the function when calling it, since I can have hundreds of different parameters types combination depending on the function called.

Any hint how that could be achieved without having hundreds of nested if statements to cast the parameters to the correct types before calling the functions?

That functionality is similar to what GDB has, where you can do call foo(2,'a') and GDB calls that function to you.

like image 400
J. In Avatar asked May 13 '16 04:05

J. In


People also ask

What kind of parameters are passed during function call?

Formal Parameter : A variable and its type as they appear in the prototype of the function or method. Actual Parameter : The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment. Modes: IN: Passes info from caller to callee.

Can I call a function without parameter?

You can use a default argument in Python if you wish to call your function without passing parameters. The function parameter takes the default value if the parameter is not supplied during the function call.

Does a function always have to ask for parameters?

Answer. Functions will still work as normal even if none of the parameters are actually used within the function code itself. However, doing this may be counterintuitive, as the purpose of parameters is to allow different input values to be used when running a function to produce results based on the input.

What do you call a parameters received by a function?

The parameter values, called arguments, are passed to the function when the function is called.


1 Answers

There are two approaches to this. If what you described is all you want to do, then you can use the dyncall library so that you dont have to worry about platform/compiler-specific calling semantics yourself:

The dyncall library encapsulates architecture-, OS- and compiler-specific function call semantics in a virtual bind argument parameters from left to right and then call interface allowing programmers to call C functions in a completely dynamic manner. In other words, instead of calling a function directly, the dyncall library provides a mechanism to push the function parameters manually and to issue the call afterwards.

The other approach is, if you might want to do more: e.g. what if an argument cannot be created by a literal? What if the argument is the output of another function? Can you write f(123, g("a")) in your console? Can you write x=g("a"); f(x)? And if(cond) x="a" else x="b"; f(x) In this case you need to embed a scripting language like e.g. LUA.

like image 131
Bernd Elkemann Avatar answered Oct 05 '22 22:10

Bernd Elkemann