我试图使用IAR EWARM编译下面的C代码,但是我得到了三个编译错误(ErrorPe028: expression必须有一个常量值)。如下所示:
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
typedef uint8_t I2C_BusIdentifier;
typedef uint8_t I2C_SlaveAddress;
typedef enum {
I2C_BUS_STATE_UNINITIALIZED = 0,
I2C_BUS_STATE_GPIO_HARDWARE_READY,
I2C_BUS_STATE_READY_TO_OPERATE,
} I2C_BusState;
typedef struct BUS_I2C_BUS_INSTANCE_TYPE {
I2C_BusIdentifier BusIdentifer; // 0 for I2C0, 1 for I2C1
I2C_BusState CurrentState; // bus status
} I2C_Bus; // I²C Bus Instance Type, I2C_BusInstanceType
typedef struct DEVICE_I2C_GENERIC {
I2C_Bus* DeviceBusPointer;
I2C_SlaveAddress DeviceAddress;
} I2C_Device;
// inherits from I2C_Device
typedef struct DEVICE_ADC123 {
I2C_Device Device;
} ADC123_Device;
#define NUMBER_OF_I2C_PORTS 2
static I2C_Bus g_I2C_Bus[NUMBER_OF_I2C_PORTS] = {
{ 0, I2C_BUS_STATE_UNINITIALIZED, },
{ 1, I2C_BUS_STATE_UNINITIALIZED, },
};
I2C_Bus* const g_I2C_BusPtr_Port0 = &(g_I2C_Bus[0]);
I2C_Bus* const g_I2C_BusPtr_Port1 = &(g_I2C_Bus[1]);
const ADC123_Device g_Device_ADC123_U14 = {
{ g_I2C_BusPtr_Port0, 0xAE, }, // <--- Error[Pe028]: expression must have a constant value
};
const ADC123_Device g_Device_ADC123_U15 = {
{ g_I2C_BusPtr_Port1, 0x8A, }, // <--- Error[Pe028]: expression must have a constant value
};
const ADC123_Device g_Device_ADC123_U9 = {
{ g_I2C_BusPtr_Port1, 0xAA, }, // <--- Error[Pe028]: expression must have a constant value
};
#define NUMBER_OF_ADC123_DEVICES 3
const ADC123_Device* g_ADC123_Array[NUMBER_OF_ADC123_DEVICES] = {
&g_Device_ADC123_U14,
&g_Device_ADC123_U15,
&g_Device_ADC123_U9,
};
int main(void)
{
while(1);
}但是,如果我直接使用g_I2C_Bus地址而不是通过g_I2C_BusPtr_PortX指针,一切都可以正常编译:
const ADC123_Device g_Device_ADC123_U14 = {
{ &(g_I2C_Bus[0]), 0xAE, },
};
const ADC123_Device g_Device_ADC123_U15 = {
{ &(g_I2C_Bus[1]), 0x8A, },
};
const ADC123_Device g_Device_ADC123_U9 = {
{ &(g_I2C_Bus[1]), 0xAA, },
};我想使用常量指针(g_I2C_BusPtr_Port0,g_I2C_BusPtr_Port1),因为它们在.h文件中是外部的,而数组(g_I2 C_Bus[])不会全局公开,而是在特定的.c文件中静态公开。
为什么强迫者对此不满意,因为定义/值应该是相等的,因为它们引用了相同的东西?
发布于 2015-01-21 23:35:43
这是C语言的一个限制。变量的值,如
int const a = 1;不能在常量表达式中使用,例如用作初始值设定项:
int b = a; /* Will not work */即使使用const限定符也不行。原因是编译器无法知道变量的值,即使它看起来非常微不足道。带有const的变量不是C中的常量,它是唯一不能被你改变的变量。
全局变量的地址是另一回事。链接器完全控制这些变量所在的位置,并且可以对初始化器使用相同的信息。
解决方法是使用预处理器:
#define g_I2C_BusPtr_Port0 (&(g_I2C_Bus[0]))发布于 2015-02-05 07:38:04
如果从其他答案中还不清楚,正确的代码组织是:
// stuff.h
//
extern I2C_Bus *const g_I2C_BusPtr_Port0;
extern I2C_Bus *const g_I2C_BusPtr_Port1;然后:
// stuff.c
//
#include "stuff.h"
I2C_Bus* const g_I2C_BusPtr_Port0 = &g_I2C_Bus[0];
I2C_Bus* const g_I2C_BusPtr_Port1 = &g_I2C_Bus[1];
const ADC123_Device g_Device_ADC123_U14 =
{ { &g_I2C_Bus[0], 0xAE } };诸若此类。
发布于 2015-01-22 00:16:55
静态对象的初始化器必须是常量表达式或字符串文字,例如,如果ADC123_Device g_Device_ADC123_U14是自动变量,那么就不会有错误,例如,如果您在main中声明了它。我们可以通过转到draft C99 standard部分6.7.8 Initialization看到这一点,该部分写道:
具有静态存储持续时间的对象的初始值设定项中的所有表达式都应为常量表达式或字符串文字。
第二种情况有效的原因是允许常量地址,我们可以从6.6常量表达式中看到这一点,它表示:
允许初始值设定项中的常量表达式具有更大的自由度。这样的常量表达式应为或计算为以下值之一:
并包括以下项目符号:
https://stackoverflow.com/questions/28070327
复制相似问题