簡體   English   中英

錯誤C2099:初始化程序不是常量

[英]error C2099: initializer is not a constant

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應用程序。

在嘗試使用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 Studios Win32環境中應產生相似的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM