c语言 函数指针编译出错

#ifndef LIBAO_H
#define LIBAO_H

typedef enum{wav,wim,mp3} io_type_t;
typedef struct IOINFO{
io_type_t type; //文件类型编号
char *name; //wav wim,mp3等
char *author; //作者
char *time; //编写时间

char *describe; //模块描述

} io_info_t;

typedef struct LIBIO{
io_info_t info; //模块信息
int (*ropen)(char *devname,int *sample,short *channel,short
bit) //打开文件
{
printf("1\n");
}

} io_t;

extern io_t *getfile(io_type_t type);

#endif

libio.h:16: 错误:expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token

函数指针是指向函数的指针变量,即本质是一个指针变量。你把他当成了函数并且去实现了,不错才怪!!!
int (*f) (int x); /* 声明一个函数指针 /
f=func; /
将func函数的首地址赋给指针f */

 int (*ropen)(char *devname,int *sample,short *channel,short bit) //打开文件
{
};

这里把;去掉


 extern io_t *getfile(io_type_t type);

这么写肯定不行的,getfile这个函数你要去实现它,不然这个函数是未定义的

你看看你报错的是不是最后一行还是最后一行附近

在结构体中定义了一个函数指针,即ropen。接下来的{}是希望定义函数体。这里直接这样子定义:

typedef struct LIBIO{
io_info_t info; //模块信息
int (*ropen)(char *devname,int *sample,short *channel,short bit); //打开文件
} io_t;

不要把函数实现放在这里,即把接口和具体实现进行分离(解耦)。

下面这个是声明,为结构体的一个成员:

int (*ropen)(char *devname,int *sample,short *channel,short bit);

接下来的这个类似于函数定义:

{
printf("1\n");
}

抛开函数声明与函数指针赋值,可以做一个简单的验证:

struct XYZ {
    int x = 1;
};

即像这个逻辑非常简单的示例代码,编译都是有问题的。即struct中不允许初始化非const类型的数据类型。下面这个是可以的:

struct XYZ {
    const int x = 1;
};

因此通过这种简化问题,可以确定问题的原因是什么。

下图是C99标准中对struct的语法描述,可以看到在struct中只能是declarator或者是常量表达式(如前面的const int x = 1;这种)。

struct

改了之后还有错的话,发之后的错误是什么

http://blog.csdn.net/qq_21792169/article/details/50809501http://blog.csdn.net/qq_21792169/article/details/50436089

#ifndef LIBAO_H
#define LIBAO_H
typedef enum{ wav, wim, mp3 } io_type_t;
typedef struct IOINFO{
io_type_t type; //文件类型编号
char *name; //wav wim,mp3等
char *author; //作者
char *time; //编写时间

char *describe; //模块描述

} io_info_t;
typedef struct LIBIO
{
io_info_t info; //模块信息
int *ropen(char *devname, int *sample, short *channel, short bit) //打开文件
{
printf("1\n");
}
} io_t;
extern io_t *getfile(io_type_t type);
#endif