H
hizostudio
Hi there,
I have a problem.
I'd like to make a function (*callback_func*), which returns another
one (*callback*) (according to the arguments of the first one), in
order to give it to another function (*get*), which accepts only a
precise type of function and will use it as a callback function.
I don't know if it's very speaking for you, so I'll try to give you an
example of such functions.
- I use GCC (and its extension for the nested functions)
- I didn't write the prototypes
---------------------------------------------------------------
#include <stdlib.h>
#include <stdbool.h>
bool get_first_int(int * const ptr, const int list[], const size_t
size, bool (*func)(const int))
{
bool found;
unsigned int i;
for(found = false, i = 0; !found && i < size; i++)
{
if(func(list))
{
*ptr = list, found = true;
}
}
return found;
}
bool is_odd(const int i)
{
return i & 1;
}
bool is_multiple_of_3(const int i)
{
return is_multiple_of(3)(i);
}
bool (*is_multiple_of(const int multiple))(const int)
{
bool func(const int i)
{
div_t d = div(i, multiple);
return d.quot != 0 && d.rem == 0;
}
return func;
}
---------------------------------------------------------------
Here, *callback_func* is "is_multiple_of", *callback* are "is_odd" or
"is_multiple_of_3" and *get* is "get_first_int".
Then, I'd like to write :
---------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void)
{
int res, list[7] = {0, -6, 2, 3, 6, 1, 5};
bool found;
found = get_first_int(&res, list, sizeof(list) / sizeof(*list),
is_multiple_of(5));
if(found)
{
printf("First match is '%i'\n", res);
}
else
{
puts("No match...");
}
return EXIT_SUCCESS;
}
---------------------------------------------------------------
I want to give as callback function, to *get*, directly the function
returned by *callback_func*, and not a global function (such as
"is_multiple_of_3").
But, the problem is that it won't work, since the function returned by
*callback_func* is local and thus the memory zone is inaccessible in
my *get* function.
So, I'd like to know how to fix this, properly.
Thanks in advance !
Cordially,
Hiz0
PS: Sorry for my bad english, I'm french !
I have a problem.
I'd like to make a function (*callback_func*), which returns another
one (*callback*) (according to the arguments of the first one), in
order to give it to another function (*get*), which accepts only a
precise type of function and will use it as a callback function.
I don't know if it's very speaking for you, so I'll try to give you an
example of such functions.
- I use GCC (and its extension for the nested functions)
- I didn't write the prototypes
---------------------------------------------------------------
#include <stdlib.h>
#include <stdbool.h>
bool get_first_int(int * const ptr, const int list[], const size_t
size, bool (*func)(const int))
{
bool found;
unsigned int i;
for(found = false, i = 0; !found && i < size; i++)
{
if(func(list))
{
*ptr = list, found = true;
}
}
return found;
}
bool is_odd(const int i)
{
return i & 1;
}
bool is_multiple_of_3(const int i)
{
return is_multiple_of(3)(i);
}
bool (*is_multiple_of(const int multiple))(const int)
{
bool func(const int i)
{
div_t d = div(i, multiple);
return d.quot != 0 && d.rem == 0;
}
return func;
}
---------------------------------------------------------------
Here, *callback_func* is "is_multiple_of", *callback* are "is_odd" or
"is_multiple_of_3" and *get* is "get_first_int".
Then, I'd like to write :
---------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void)
{
int res, list[7] = {0, -6, 2, 3, 6, 1, 5};
bool found;
found = get_first_int(&res, list, sizeof(list) / sizeof(*list),
is_multiple_of(5));
if(found)
{
printf("First match is '%i'\n", res);
}
else
{
puts("No match...");
}
return EXIT_SUCCESS;
}
---------------------------------------------------------------
I want to give as callback function, to *get*, directly the function
returned by *callback_func*, and not a global function (such as
"is_multiple_of_3").
But, the problem is that it won't work, since the function returned by
*callback_func* is local and thus the memory zone is inaccessible in
my *get* function.
So, I'd like to know how to fix this, properly.
Thanks in advance !
Cordially,
Hiz0
PS: Sorry for my bad english, I'm french !