| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3600 人关注过本帖
标题:结构体内定义函数怎么编译报错
只看楼主 加入收藏
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
#include <stdio.h>


struct class_t;
typedef struct class_t class_t;;

typedef struct data {
    int i;
    class_t * class;
} data;


struct class_t
{
    int (* print)(const char *, ...);
};

class_t funclass = {printf};

int main(void)
{

    data a;
    a.class = &funclass;
    a.i = 10;

    a.class->print("%d", a.i);
   
    getchar();
    return 0;
}

—>〉Sun〈<—
2008-06-23 22:56
lingluoz
Rank: 2
来 自:苏州科技学院
等 级:新手上路
威 望:4
帖 子:749
专家分:0
注 册:2008-2-2
收藏
得分:0 
LS解释一下代码好吗。。那个指针型函数的调用应该是(*pf)()这种形式的吧。。为什么可以直接调用呢。

Murphy's Law :
If there are two or more ways to do something, and one of those ways can result in a catastrophe, then someone will do it.
2008-06-24 08:31
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#include <stdio.h>
struct class_t;
typedef struct class_t class_t;

typedef struct data {
    int i;
} data;
   
int (* print)(const char *, ...);

int _tmain(int argc, _TCHAR* argv[])
{

    data a;
    a.i = 10;
    print=printf;
    (*print)("%d\n", a.i);
    print("%d\n", a.i);
    getchar();
    return 0;
}
这两个都是对的

学习需要安静。。海盗要重新来过。。
2008-06-24 09:05
快速回复:结构体内定义函数怎么编译报错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011293 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved