2

协议.h

typedef struct sDevice_d
{
    char name[24];
    signed int (*Send)(unsigned char*, unsigned short);
    signed int (*Recv)(unsigned char*, unsigned short);
} sDevice_d, *psDevice_d;

设备.c

#include "Protocol.h"

sDevice_d sDevice = { "ten", I2c_Send };
psDevice_d psDevice = &sDevice;

static signed int I2c_Send(unsigned char* buf, unsigned short len)
{
    return 0;
}

在上面的代码中,我收到以下错误:

错误 C2099:初始化程序不是常量

请帮我解决这个问题。

我正在使用 Visual Studio Win32 应用程序。

4

1 回答 1

4

函数 I2c_Send 需要在其初始化列表中使用它来创建和初始化结构之前定义和可见。我在下面对您的代码进行了改编,在两个文件中说明了这一点:

协议.h

typedef struct
{
    char name[24];
    signed int (*Send)(unsigned char*, unsigned short);
    signed int (*Recv)(unsigned char*, unsigned short);
} S_DEVICE;

//prototype here
static signed int I2c_Send(unsigned char* buf, unsigned short len); 

设备.c

#include "protcol.h"

S_DEVICE sDevice_d = {"ten", I2c_Send, I2c_Send}; 

int main(void)
{
   return 0;
}

//define here
static signed int I2c_Send(unsigned char* buf, unsigned short len)
{
    return 0;
}

此源代码使用 ANCI C 编译器(使用 C99 扩展)编译和构建,在您的 Visual Studios、Win32 环境中应该会产生类似的结果。

于 2013-09-11T16:06:32.743 回答