How to pass function pointer to function in c? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can anybody please guide me how to pass a function pointer to a function as argument one with second argument being int*? The function then calls a function whose pointer is passed as first argument by passing the
second argument to it.

I Can give you this example
int plus_one(int nb)
{
return nb + 1;
}
int call_func(int (*func)(int), int *param)
{
return (*func)((*param));
}
I Think what you want is the call_func functions, so the important is to pass as first paramaeter something with this format :
return_type (*name_you_give_to_the_function)(parameter_type1, parameter_type2, ...) //parentheses to indicates that is a function
So with that you can make function calling any type of function
to call your call function , just write the name of your function as first parameter :
Exemple link to the previous one
int main(void)
{
int nb = 3;
int res = call_func(plus_one, &nb);
printf("res: %d\n", res);
}

A function with an int * parameter is declared as void FunctionName(int *). (You can use something other than void for the return type.)
Then a pointer to such a function is declared as void (*PointerName)(int *).
So to declare a function with a parameter of this type, we use that declaration for the parameter: void CallingFunctionName(void (*PointerName)(int *), int *IntegerName).
Here is an example:
void CallingFunction(void (*PointerToFunction)(int *), int *Q)
{
PointerToFunction(Q); // Call the function and pass it Q.
}
Notes
Note that to call a function by a pointer, you just have to write a function call using the pointer, PointerToFunction(Q). You do not have to use * to make the pointer into a function, as in (*PointerToFunction)(Q). All function calls are actually done through pointers, and, when you use a function name in a function call, it is automatically converted to a pointer anyway. So, when you already have a pointer, just use it.
Similarly, because you can only pass a pointer to a function as a parameter, not an actual function, if you use a function declaration for a parameter, it will be automatically adjusted to be a pointer to a function. This means we can change the example to:
void CallingFunction(void PointerToFunction(int *), int *Q)
{
PointerToFunction(Q);
}

Related

Global variable as an argument of function call in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
"While passing global variable in function parameter, is it passed by reference or value ?"
It is passed by value. The following code shows that this is the case:
#include <stdio.h>
int global = 5;
void foo(int bar){
bar = 6;
printf("bar = %d\nglobal = %d", bar, global);
}
int main(){
foo(global);
return 0;
}
The output is:
bar = 6
global = 5
In this code global was passed as a parameter for foo, we called this parameter bar. So at the beginning global and bar are two different variables both having the value 5. But then bar is assigned the value 6 and since the argument was referenced by value, global stays at 5.
To pass the variable by reference, use pointers:
#include <stdio.h>
int global = 5;
void foo(int *bar){
*bar = 6;
printf("bar = %d\nglobal = %d", *bar, global);
}
int main(){
foo(&global);
return 0;
}
Now the output is:
bar = 6
global = 6
Both local and global variables are passed to functions by value in C. If you need to pass by reference, you will need to use pointers.
How the variable is/must be passed, depends on the function, not on the variable:
int gMyVar;
void foo(int a); // says "call me by value"
void bar(int *b); // says "call my by reference"
foo requires an int to be passed. you must call it as foo(gMyVar).
bar requires a pointer to an int. You must call it as bar(&gMyVar).
So, as other answers indicated, C always passes a value, however, the value can be the value of a variable (call by value) or can be a pointer to a variable (call by reference).
First of all, Why do we need to pass Global variable in a function? we can directly access it any where in the program if you dont know.
#include <stdio.h>
int x = 10; //global variable
void fun()
{
printf("%d",x); // direct access
}
int main(void) {
fun(); // no argument required
return 0;
}
Output
10
For demo http://ideone.com/VLWqNO

Using a void type procedure to change a variable in C

I need to figure out how to use a void type function to change a value in another function, so I'm trying to write a practice program that uses a procedure to change an integer from 5 to 4 and then prints the new integer (should be 4).
#include <stdio.h>
#include <stdlib.h>
void change(int x)
{
x = 4;
}
int main(int argc, char **argv)
{
int z = 5;
change(z);
printf("%d\n",z);
return 0;
}
This prints 5 at the end. I can tell there's some kind of issue with scope here, but I can't figure out how to resolve it. I also can't print within the procedure, so that solution is out of the question. I'd really appreciate any help!
To change a variable within another function, that isn't in the scope of the function, you must pass the variable by pointer.
void change(int *x)
{
*x = 4;
}
And call the function using change(&z).
If the variable isn't passed by pointer, then only the variable inside the scope of the function will change, but not its argument.
In C, function arguments are always passed by value. This means that any changes made to a value in a function are not reflected in the caller. That is what's happening in your case.
Fortunately, you can pass a pointer (by value of course) instead. This allows you, via dereferencing, to change the value that the pointer is pointing to.
To do this, adjust the prototype of your function to
void change(int* x)
Then, within that function, use
*x = 4;
And, finally, call the function using
change(&z);
You need to pass the address of the variable and then you can change the value of the variable.
void change(int *x)
{
*x = 4;
}
Now the invoking function will have new value of x which is 4.
You can pass a pointer to the function, like so:
void change(int *x)

Return function pointer to function that returns the same type [duplicate]

This question already has answers here:
Function Returning Itself
(10 answers)
Closed 8 years ago.
In C, how can I declare a function that returns a function that returns a function etc.
I.e., I want something like this
typedef A (*A)();
A a = ...
a()()()()();
I want to achieve the following C++ behavior:
struct A { A operator()() { return A(); } };
A a;
a()()()()();
You cannot return a function in C - you return a pointer to a function. If you mean to define a function which returns a pointer to a function which again returns a pointer to a function and so on, then you can use typedef to implement it.
typedef int (*f)(int);
typedef f (*g)(float);
typedef g (*h)(char);
// and so on
However, if you mean to define a function which returns a pointer to a function of its own type, then no you cannot do it because you cannot define a recursive type in C. See here for details - Function Returning Itself
The answer is "almost yes".
Take a look the the answer given by Drew McGowen in response to Function Returning Itself.
I think that answer presents code that is closest to the behavior you are trying to see.
To have a function return a function of its own type one needs to use a intermediate function type.
As by the C Standard it is allowed to cast a function variable to any other function variable and back the following approach would do:
typedef int (*T)(void); /* The type of desire. */
typedef void (*_T)(void); /* The intermediate type. */
int g(void)
{
return 42;
}
_T h(void)
{
return (_T) g;
}
int main(void)
{
T f = (T) h;
int a = f();
int b = ((T) h())();
}
a and b both get 42assigned.

Error Invalid use of void expression

I have a function int rt_task_start (RT_TASK *task, void(*task_func)(void *arg), void *arg)
where in second argument i am passing a function with argument.
When i only pass a function name at that time there is no problem.(as expected it's working). rt_task_start(&demo_task1, demo, 1);
But when i pass rt_task_start(&demo_task1, demo(&val), 1); it's giving me error error: invalid use of void expression. Variable val is defined before. int val = 0;
When i call with this rt_task_start(&demo_task1, demo(val), 1); this is showing error Warning passing argument 1 of 'demo' makes pointer from integer without a cast then error: invalid use of void expression.
int *val;
*val = 0;
rt_task_start(&demo_task1, demo(&val), 1); this is also giving me error.
I can't understand what should i pass, as a void pointer. It's giving me error. Any Idea Please!
void (*task_func)(void *arg);
The above statement defines task_func to be a pointer to a function which takes a pointer of type void * and returns no value.
Therefore, when you call your function rt_task_start, you should pass a pointer to a function as the second argument. Also, you should pass a pointer of type void * as the third argument, not an integer. A function name evaluates to a pointer to the function, so you can simply pass the function name as the argument value, or you can use the address-of operator & before the function name.
int arg = 4;
// both calls are equivalent
rt_task_start(&demo_task1, demo, &arg);
rt_task_start(&demo_task1, &demo, &arg);
I'm not sure how the code in (1) can possibly compile. But here is what you should be using:
int rt_task_start (RT_TASK *task, void(*task_func)(void *arg), void *arg);
int val = 1;
rt_task_start(&demo_task1, demo, &val);
You cannot pass the function pointer bound to a specific argument, that is something like a closure, which isn't available in C. You can, however, pass the function pointer and then separately pass the argument you want to apply (which is what the function signature suggests you should do). But you must pass that argument as a pointer, not a literal.
Surely you want:
int i=1;
rt_task_start(&demo_task1, demo, (void*) &i);
Just by matching the argument types, remember the second argument is just a function pointer, not a function call with its own argument, it's own argument is only used when you call it within rt_task_demo. If you then want to use the value '1' in function 'rt_task_demo' you would recast it like
int ii = *(int*) arg;

Pointer to a function that takes a pointer to another function as argument

This should be a simple question, but I might not be able to word it correctly or I might be trying to defy the principles of the C language because of my lack of experience with it.
All I want to do is, given a pointer to a function, to wrap it inside another function that takes a pointer to a function as an argument, and make a pointer of the later. Better in code:
void do_nothing_1() {}
void do_nothing_2() {}
void wrapper(void(*f)()) { f(); }
int main() {
// the following will call the function
// but i just want a pointer of whats being called
funcion_pointer_1 = wrapper(do_nothing_1);
funcion_pointer_2 = wrapper(do_nothing_2);
return 0;
}
I apologize beforehand if the question doesn't make any sense, please help me clarify it rather than simply downvote it.
Edit: Given the apparent difficulty to obtain the results desired because of the obscurity of the requirements, I will be a little more specific on what I am looking for.
Inside a struct, I have a pointer to a function:
struct my_struct {
int (* run)(void);
}
What I want to do is to modify that function and that's why I use the wrapper. So, for example, if foo returns void change that to int. Also, run some code before executing foo:
int wrapper(void (*foo)()) {
// exec some stuff here
foo();
return 0;
}
Ultimately, what I want is that when I execute the function corresponding to the *run pointer from my structure, execute some stuff before doing run() and change the return type.
When struggling with the correct syntax, it is helpful to use typedefs to make things clearer. Not just for yourself as you write the code, but for anyone who needs to maintain it, or just try to figure out what it's doing.
You want a function pointer to a function that takes another function pointer as an argument. Start with the argument, a function without parameters and no return value:
typedef void (*void_handler_t)( void);
And now the wrapper function pointer, one that takes a function pointer parameter and has no return value:
typedef void (*wrapper_handler_t)( void_handler void_fn);
And the code:
void foo( void) {}
void wrapper( void_handler_t wrapped_fn)
{
wrapped_fn();
}
int main( int argc, char *argv[])
{
wrapper_handler_t function_pointer;
function_pointer = &wrapper;
function_pointer( &foo);
return 0;
}

Resources