The following is an introduction to the implementation of the struct function in C. Only function pointer members can be used. The C structure body does not have function code, but can have function pointers. C/C codeCode highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/
#include <stdio.h> struct test { void fun() { printf("hello,world\n"); } }; int main() { struct test _t; _t.fun(); return 0; }
The above code is saved as. c, which is not available in VC 6.0 and Dev Cpp. Function pointer implementation, rather than directly defining functions... Of course, struct can put function pointers. For example: C/C codeCode highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/
#include <stdio.h> void fun() { printf("hello,world\n"); } struct test { void (*Fun)(); }; int main() { struct test _t; _t.Fun = fun; (*_t.Fun)(); return 0; }
The C structure body does not have function code, but it can have function pointers. The netizen replied: struct in pure C does not have a member function, but it can have function pointers. Object-oriented programming with ANSI-C is used to simulate member functions with function pointers. For more information about how to use the C language in Linux source code, see the Linux kernel source code.
#include<stdio.h> struct MyClass { char* name; int age; void (*funnull) (); void (*func) (struct MyClass mc); }; void realfunnull() { printf("hello world!\n"); } void realfunc(struct MyClass mc) { printf("MyClass's name is:%s\n",mc.name); printf("MyClass's age is:%d\n",mc.age); } int main() { struct MyClass mc = {"Simon", 25, realfunnull, realfunc}; mc.funnull(); mc.func(mc); return 0; }