Re: Function Pointers



bwaichu@xxxxxxxxx wrote:
On Jan 20, 11:25 pm, Wang Jiaji <jiaji.w...@xxxxxxxxx> wrote:

In the machine code, the function's pointer and the function's name
are really the same thing, they're both the start point of a block of
instructions. The only difference is that, _in the C code_, you can't
reassign the function name to another block of instructions, while you
can with the function pointer. Unfortunately, your sample code missed
the only purpose of this candy syntax in C :p

I write assembly, so I'm used to returning out of all functions
(whether I set eax or not on x86 machines), and I am aware of calling
memory locations that contain functions from dlls, rather than
importing them.

But I struggle with practical usage and examples of function
pointers. I want to make sure I am getting the syntax right, and I
would like to know when I should use them. I think, the only place I
have seen them used is in libpcap.

Well, one practical use of function pointers is, as Ben pointed out, its use as a callback functions. Arrays of function pointers are also useful. For example, you could have a set of signals:

int rts(void *data);
int cts(void *data);
int txd(void *data);
int rxd(void *data);

a typedef:

typedef int (*fp)(void *data);

an array of function pointers:

fp mysignals[] = {rts, cts, txd, rxd};

and use them as:

int err;
err = mysignals[0](NULL);
err = mysignals[2]((void *)"SOMETHING");

where the signals could be defined as:

int rts(void *data)
{
/* ignore data, do seomthing */
return 0;
}

int txd(void *data)
{
char *buf = (char *)data;
/* do something with buf */
return 0;
}

From what you had posted, I take it that you have got the syntax right. As for where you should use function pointers, well, that depends a lot on what you are trying to do.


Regards,
Vi
.



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)
  • [PATCH 2/3] Sysfs: Allow directories to be populated dynamically
    ... Use function pointers to populate and depopulate sysfs directories with ... dynamically files. ... const struct attribute_group *grp) ... static int internal_create_group(struct kobject *kobj, int update, ...
    (Linux-Kernel)
  • 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: pointer
    ... wondering what you would use this syntax for: ... int; ... What's the added pointer versatility ... can initialise an array of function pointers, ...
    (comp.lang.c)