Reputation: 43
i have a simple program like this :
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*add)(int *))
{
if (nb < 5)
f(nb, add(&nb));
return (nb);
}
int main() {
int b = 5;
int a = f(b, add);
printf("%d\n", a);
}
i want to call f recursively until nb become greater or equal to 5, but when i compile the program , the gcc compiler show something like this :
error: passing 'void' to parameter of incompatible type 'void (*)(int *)'
can anyone help me please?
Upvotes: 0
Views: 67
Reputation: 4370
I'm not completely certain as to what you are trying to do with your function, but I took a stab at what I think you might be attempting to do:
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*function)(int *))
{
function(&nb);
if (nb < 5)
f(nb, function);
return (nb);
}
int main() {
int b = 5;
int a = f(b, add);
printf("%d\n", a);
}
Now your function f
accepts a function-pointer to a void function that takes an int *
as an argument (I renamed this argument to function
so it doesn't conflict with your existing function add
). It proceeds to call said function and pass in a pointer to nb
to function
.
What you were doing was passing in the result of add(&nb)
into the function f
as an argument (which said function is a void so it does not return anything) instead of passing in a pointer to the function.
Upvotes: 3
Reputation: 11817
As Christian pointed out in the comments, the problem is on the recursive call line:
f(nb, add(&nb));
If I understand correctly you are trying to increase nb
using the function you receive as a pointer; here however you first pass nb
unchanged (and as a value, not by reference), and then call the add function.
In the second argument the compiler is expecting a function pointer, but instead it receives the return value of the add(&nb)
function call, which is void
.
Also, in case nb is < 5 you need to return the recursive call.
The correct procedure would be:
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*add)(int *))
{
if (nb < 5) {
add(&nb);
return f(nb, add);
}
return (nb);
}
int main() {
int b = 1;
int a = f(b, add);
printf("%d\n", a);
}
It almost seems like you're trying to use lazy evaluation, but C doesn't have that!
Upvotes: 0
Reputation: 215350
The line f(nb, add(&nb));
doesn't make sense. You need to call the function somewhere, but not on the same line as where you pass the function pointer on, recursively.
Overall the program doesn't make much sense. I have no idea what you are trying to do, perhaps something similar to this?
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
void f(int* nb, void (*add)(int *))
{
add(nb);
if (*nb < 5)
{
f(nb, add);
}
}
int main() {
int a = 5;
f(&a, add);
printf("%d\n", a);
}
Will print 6 since the add
is called once. The name add
as function parameter to f
is also mighty confusing, so better come up with another name for it.
Upvotes: 1
Reputation: 21572
Passing add(&nb)
as an argument passes the returned value of add
with the argument &nb
. The function signature for f
requires a function pointer, which can be passed as:
f(nb, add)
Also, even if you were actually trying to pass the returned value of add
, that would be undefined behavior since it is of type void
.
Upvotes: 0