Ah've got them Function Pointer blues



Folks,

I've been playing with C programs for 25 years (not professionally -
self-taught), and although I've used function pointers before, I've never
got my head around them enough to be able to think my way through what I
want to do now. I don't know why - I'm fine with most other aspects of the
language, but my brain goes numb when I'm reading about function pointers!

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.

I would like to initialise the array directly, like
struct FS function_structure[] = {{ ....}, {....}, ...., {....}};

.... but I'm really shaky on what to put in those braces. I have been
playing with it, but so far unsuccessfully.

Is it possible? How do I declare the pointer in the structure, and how do I
assign a pointer value to it? And when I call it, how do I pass arguments
from the program? Could somebody give me a very simple example?

This isn't a homework thing, by the way - I write mostly little programs
that get used at work by colleagues. It improves my street-cred
(geek-cred? - well, no - I've just blown that notion!).

Incidentally, this may not be the best way to design the program, so I'd
like to ask about that too.
Can anybody suggest a newsgroup? (program architecture isn't really C)

Thanks,

MikeC


--
Mental decryption required to bamboozle spam robots:

mijewen$btconnect*com
$ = @
* = dot


.



Relevant Pages

  • Re: location of stack and heap varies -- why?
    ... int main{ ... C doesn't provide a way to print function pointers, ... were the same from execution to execution. ... different addresses for the global variables, ...
    (comp.unix.programmer)
  • Re: one function (in a library) - two (or more) names
    ... |>> Since function pointers can be used just like functions, ... And the compiler needs to know it is a pointer to a function, ... int; ... ldr r3, .L2 ...
    (comp.unix.programmer)
  • Re: Function Pointers
    ... reassign the function name to another block of instructions, ... I want to make sure I am getting the syntax right, ... Well, one practical use of function pointers is, as Ben pointed out, its use as a callback functions. ... int rts; ...
    (comp.lang.c)
  • Re: C aptitude
    ... pointer to a 3 element array of function pointers with a return value ... of int and a single parameter of a pointer to char ... If there ever was a good reason I would not do it like that since it can be made far clearer by using a typedef for the function type. ... void func(int (*y),int* x) ...
    (comp.lang.c)
  • Re: function pointers
    ... > Can anyone tell me the advantages of using function pointers? ... //menu items DJGPP snippet ... int bogus1; ...
    (comp.lang.c)