Re: obj function hello()



On Feb 9, 1:30 am, Ian Collins <ian-n...@xxxxxxxxxxx> wrote:
You can't have functions in structs in C. What yo can have is function
pointers:

struct Obj {
int someInt;
int (*someFn)(void);

};

But you have to assign a function address to the pointer for each
instance of Obj.

So this means i have to do something like this then ?

#include <stdio.h>

struct obj {
char *data = ''
int (*add)(void);
}

function add(char *value){
obj.data += value;
}

obj function hello(){
obj.data = 'hello';
return obj;
}

void main(){
test = hello();
test.add('world');
printf(test.data);
}



.



Relevant Pages

  • renee.c
    ... typedef struct AttrString *AttrString; ... Tcl_Obj *Obj; ... int red, green, blue, index; Colour next; ... chars name; ...
    (comp.lang.tcl)
  • Re: Double Linked List test
    ... int main ... boolean test(struct Obj *l, char *value_wanted) ... struct Obj *p_link; ...
    (comp.lang.c)
  • Re: Does malloc() reuse addresses?
    ... So all pointers in the list are always valid. ... struct obj *next; ... void deleteobj{ ...
    (comp.lang.c)
  • Double Linked List test
    ... int main ... boolean test(struct Obj *l, char *value_wanted) ... struct Obj *p_link; ...
    (comp.lang.c)
  • Re: unchecked call to compareTo(T)
    ... Hendrik Maryns wrote: ... Node(T obj) { ... found: int ...
    (comp.lang.java.programmer)