Requesting sample code!



Hello,

I am looking into pointers to functions and I am looking at stuff like this:
==================
#include <stdio.h>

int any_function(int(*pfun)(int, int), int x, int y){
return pfun(x, y);
}

int sum(int x, int y){
return x + y;
}

int product(int x, int y){
return x * y;
}

int difference(int x, int y){
return x - y;
}

int main(void){
int a = 10;
int b = 5;
int result = 0;
int (*pf)(int, int);
pf = sum; /* Pointer to sum function */
result = any_function(pf, a, b);
//result = any_function(sum, a, b);
printf("\nresult = %d", result );

result = any_function(product,a, b);
printf("\nresult = %d", result );

printf("\nresult = %d\n", any_function(difference, a, b));
return 0;
}
===========================

But I am looking all over for sample code which embeds the functions or
pointers to function in structures. In other words I am looking for sample
code which would show how to include funtions or pointers to functions as
members of a struct. Very similar ... you know like we do in C++ when we have
methods in our classes. I want to do the same thing with structures. Is this
possible and can someone direct me to some samples or books which illustrate
on how to code this.

I would show an example but I don't even know where to start to code this,
but I know it is possible because I have seen something similar to what I
want to do at:

http://bytes.com/forum/thread583012.html

But I would like a full description of how to implement functions and
pointers to functions as members of a struct which is not posted as a
question but rather as a tutorial or book!

All help and suggestions sincerely appreciated!

P.S. I have not yet looked at amazon.com for any potential books!

--
Best regards
Roberto
.



Relevant Pages

  • Re: A taxonomy of types
    ... I am describing how to represent the representation (and, ... if the system follows static typing rules (such as in a compiler), ... so, the hardware sees pointers and pointer arithmetic, but the compiler ... "Besides the char types, up to three sizes of integer, declared short int, ...
    (comp.lang.misc)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • I want my segmentation fault!
    ... no occurrences of free and a lot of routines returning pointers to ... the pointer returned by the allocator (either directly or as a component ... int length_of_list; ...
    (comp.lang.c.moderated)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • misc, refocus: new possible lang idea, BSC
    ... object //prototypical object ... it may not be possible to assign between tagged-references and pointers, ... the other, if implemented, will be based on a class/instance system. ... int foo ...
    (comp.lang.misc)

Loading