Re: Ah've got them Function Pointer blues
- From: "MikeC" <Mike.Best@xxxxxxxxxxxxx>
- Date: Thu, 21 Aug 2008 14:52:10 +0100
Gentlefolk,
You're good people. Your time and attention are very much appreciated.
Reading through it, it all makes sense - though I still have to work it into
operation.
I think the problem that I have (and maybe other people have) is that if you
have a pointer to a variable (of whatever type), and you execute
*foo = bar
you can easily see the mechanism of what happens. You'll laugh at the
analogy, but to me, it's like the postman handing the value to the variable
directly through the front door, but when it's a function pointer, the
postman comes to the door, gets on his mobile phone, and tells somebody else
to take the arguments around to the back door. They don't get to the
function by the same mechanism that gets a value to a dereferenced variable.
What I have learned, or at least had my eyes opened to from your
instructions, is that you can make a cast to a function pointer. I never
thought of that before, and I don't think I've read about it. Well, maybe I
did, but my brain was numb.
Thanks again for freely sharing your expertise.
MikeC
"Ben Bacarisse" <ben.usenet@xxxxxxxxx> wrote in message
news:87vdxvdqk1.fsf@xxxxxxxxxxxx
voidpointer <diegorocha1987@xxxxxxxxx> writes:
On 20 ago, 20:14, "MikeC" <Mike.B...@xxxxxxxxxxxxx> wrote:<snip>
I would like to have an array of structures, something like
struct FS
{ <function pointer>;
<some other variables>;
};
My problem is that the <function pointer> is of unknown type - in
function_structure[2] it may be
int func_1(int, int) {....}
in function_structure[5], it may be
void func_2(char **cpt) {....}
etc.
MikeC
--
Mental decryption required to bamboozle spam robots:
mijewen$btconnect*com
$ = @
* = dot
Best not to quote sig block (unless you are commenting on them)
as Ben Becarisse said, do make your code more readable use the
typedef,
/* funcPtr is a pointer function that has two int
as argument, and return int */
typedef int (*funcPtr)(int, int);
I prefer to not hide the pointer in the typedef. I can see you agree
because you felt the need to reveal the pointerness in the name. "*" is
shorter that "Ptr" and...
now in your structure
struct your_struct {
funcPtr func;
function *func;
Is just as easy to read (for someone who has had to "get" pointers).
It is a small point (and by no means universally agreed upon) but it
seems worth noting. It has the huge benefit that you can avoid a lot
of the (*name)(...) syntax that seems to be the stumbling block for so
many people.
};
now to initialize your structure
struct your_function t[] = {
{&anyfunction /* with the same type of funcPtr */ },
{ /* here more declarations */ }
};
now to use
(t[0].func)(argument_1, argument_2);
The OP was interested in there being different function types. You
got to show the neat version!
a more complete example
------ start ------
#include <stdio.h>
typedef int (*funcPtr)(int, int);
struct test {
funcPtr func;
int a, b;
};
int print_sum(int a, int b)
{
printf("%d\n", a + b);
return a + b;
}
int main(void) {
struct test t[] = {
{&print_sum, 2, 2},
{&print_sum, 5, 5}
};
(*(t[0].func))(t[0].a, t[0].b);
(t[1].func)(t[1].a, t[1].b);
/* the two ways is valid, but the second is more preferred
and is supported since of c89 standard */
I prefer t[1].func(t[1].a, t[1].b); because I don't like extra
parentheses, but that is also debatable.
return 0;
}
------- end --------
--
Ben.
.
- References:
- Ah've got them Function Pointer blues
- From: MikeC
- Re: Ah've got them Function Pointer blues
- From: Ben Bacarisse
- Ah've got them Function Pointer blues
- Prev by Date: Re: why are GOTO's not used ...
- Next by Date: 2d arrays!
- Previous by thread: Re: Ah've got them Function Pointer blues
- Next by thread: Re: Ah've got them Function Pointer blues
- Index(es):
Relevant Pages
|