I know, if I want OOP I should go to cpp, but I still prefer C for no real reason. Is there a C compiler that supports defining structure members that are functions? GCC by default, doesn't. I didn't try any other standards.
I know, if I want OOP I should go to cpp, but I still prefer C for no real reason. Is there a C compiler that supports defining structure members that are functions? GCC by default, doesn't. I didn't try any other standards.
Turbo C - no functions allowed in a struct.
I've never used such a compiler. You can have function pointers in your struct, though, which is a pretty good approximation--you just have to remember to point them somewhere first.
From Google: Using function pointers,
It even validates the params you pass. Is there any way to set the initial value of t.fun to get_a?Code:#include <stdio.h> struct t { int a; void (*fun) (int * a); } ; void get_a (int * a) { printf (" input : "); scanf ("%d", a); } int main () { struct t test; test.a = 0; printf ("a (before): %d\n", test.a); test.fun = get_a; test.fun(&test.a); printf ("a (after ): %d\n", test.a); return 0; }
There's the normal way of initialising a struct object, e.g.,Originally Posted by User Name:
Code:struct t test = {0, get_a};
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
o_O
Because for some reason that little "feature" is still in the C standard, I tend to downplay it. It's evil. Don't use it. So to speak.
Another way to do it is (and IMHO, the proper way):
Nevertheless, I would say it is preferable to do it in a separate function. An initializing function. The more function pointers you put, the easier it is to simply call the function instead of duplicating code.Code:struct t test = {0, &get_a};
In case if you are interested, there's GObject.
You don't need & in front of function name. It's optional.
Using C99,
Code:struct t test = { .fun = get_a }; // If I'm not wrong,other members init to proper zero according to C99
Last edited by Bayint Naung; 07-21-2010 at 05:05 AM.
Likewise, when you want to call a function through a function pointer f, remember to write (*f)(args), not f(args). The latter is evil. Don't use it. So to speak.Originally Posted by Elysia
EDIT:
Likewise, when you want to pass the address of the first element of an array x in order to simulate passing the array by reference, do not write foo(x). Rather, always write foo(&x[0]). Remember boys and girls, no exceptions. Allowing an array to be converted to a pointer to its first element is evil.Originally Posted by Elysia
Last edited by laserlight; 07-21-2010 at 05:09 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
100% agree with laserlight.
Yes, purism is evil mmkay?
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.