Re: Ah've got them Function Pointer blues



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:
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.
<snip>
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.


.



Relevant Pages

  • Re: Another spinoza challenge
    ... int main{ ... using struct and typedef. ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: [RFC][PATCH 1/6] memcg: fix pre_destory handler
    ... returns struct cgroup of id. ... SwapCgroup uses array of "pointer" to record the owner of swaps. ... struct cgroup_id is freed by RCU. ... changed interface from pointer to "int" ...
    (Linux-Kernel)
  • gcc, aliasing rules and unions
    ... struct B {int x, y;}; ... A struct pointer can be converted to another ... Also I don't know what the "effective type" of a union member is. ...
    (comp.lang.c)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)