On Feb 9, 2:41 am, Ian Collins <ian-n...@hotmail.com> wrote:
> No, you are still assuming C is an OO language. In C you have to be
> explicit:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> typedef struct Obj {
> int value;
> int (*add)(struct Obj*,int);
>
> } Obj;
>
> int add( Obj* obj, int val ) {
> obj->value += val;
> return obj->value;
>
> }
>
> int main(void) {
> Obj* obj = malloc( sizeof obj );
>
> obj->value = 0;
> obj->add = add;
>
> obj->add( obj, 42 );
>
> printf( "%d\n", obj->value );
>
> return 0;
>
> }
Thanks this is exactly what i was guessing for

I always start from
this example to learn a language. It defines for me a basic learning
path. So if something doesn't work i have this example to figure out
why a other example doesnt work. I can read about pointers, structs
and functions but that doesnt mean i understand it.
One last sub question, is this a good way to make programs in C by
defining a struct and assign function pointers to it to make it look
like a oo. Or are there other recommended methods to make a program.