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

时间:2013-09-11 13:58:14

标签: c visual-studio-2010

Protocol.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;

Device.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应用程序。

1 个答案:

答案 0 :(得分:4)

需要定义和显示函数I2c_Send 在尝试使用之前创建和初始化结构之前 它在初始化列表中。我已经包含了对您的代码的修改 下面用两个文件说明了这一点:

protocol.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); 

device.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 Studio,Win32环境中的类似结果。