我是靠谱客的博主 生动汉堡,这篇文章主要介绍c语言定义函数结构体,C中struct的函数实现,现在分享给大家,希望可以做个参考。

C中struct的函数实现,只能用函数指针成员

C/C code

Code highlighting produced by Actipro CodeHighlighter

(freeware)

#include

struct test

{

void

fun()

{

printf("hello,worldn");

}

};

int main()

{

struct

test _t;

_t.fun();

return

0;

}

上面的代码保存为.c, 在VC 6.0, Dev Cpp 里都通不过。

函数指针方式实现,而不要直接定义函数 ...

当然struct里能放函数指针的。比如这样:

C/C code

Code highlighting produced by Actipro CodeHighlighter

(freeware)

#include

void fun()

{

printf("hello,worldn");

}

struct test

{

void

(*Fun)();

};

int main()

{

struct

test _t;

_t.Fun =

fun;

(*_t.Fun)();

return

0;

}

C结构体内不能有函数的代码,但可以有函数的指针

网友回复:纯C中的struct没有成员函数,但可以有函数指针。

Object-oriented programming with ANSI-C是用函数指针来模拟成员函数的。

可以看一下这个:http://purec.binghua.com/Article/Class1/Class2/200406/228.html

最后

以上就是生动汉堡最近收集整理的关于c语言定义函数结构体,C中struct的函数实现的全部内容,更多相关c语言定义函数结构体,C中struct内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部