Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a function that takes a parameter of unknown type in C?

Tags:

c

types

Say I have the following code:

struct test* t1;
t1 = get_t(1);

... where get_t is:

struct test* get_t(int);

How can I refactor the above code and put it in a function? Something like the following:

void r1(?* t, ?* (fn*)(int)) {
    t = fn(1);
}

/* ... */

struct test* t1;
r1(t1, &get_t);
like image 388
rid Avatar asked May 17 '11 09:05

rid


People also ask

Can a function have no parameters in C?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.

How the parameters can be passed to a function?

Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.

Can you call a function without a 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.


1 Answers

use void *param, a pointer to anything ... commonly used in glib as gpointer

like image 104
Manuel Salvadores Avatar answered Oct 24 '22 22:10

Manuel Salvadores