C的误差:前pression必须有一个恒定的值 [英] C error: expression must have a constant value
问题描述
我写一些嵌入式code拥有超过SPI外部设备接口。该器件具有不同长度,并让事情简单一点我已经定义的结构如下几个寄存器
typedef结构
{
uint16_t签名:1; //注册带符号
uint16_t CommLengthBytes:3; //寄存器的字节宽度
uint16_t地址:12; //注册地址
} ts_register;
我已经定义,然后在我的消息来源的各个寄存器如下:
静态常量ts_register SAGCYC = {0,1,量0x000};
静态常量ts_register DISNOLOAD = {0,1,0×001};
静态常量ts_register LCYCMODE = {0,1,量0x004};
静态常量ts_register IRMSA = {0,4} 0x31A;
静态常量ts_register IRMSB = {0,4} 0x31B;
静态常量ts_register VRMS = {0,4} 0x31C;
等。
我将采取一个指向ts_registers数组,排队读取所有寄存器阵列中,并调用回调函数来处理回复所需的SPI传输功能
我的问题是当我试图让我想读ts_registers的排列如下:
ts_register regs_to_read [3] = {VRMS,IRMSA,IRMSB};
这会产生错误:恩pression必须有一个恒定的值3次(每个数组元素一次)。
由于他们被定义为常数,你有什么我忽略了?
由于他们被定义为常数,你有什么我忽略了?
块引用>在与
对象常量
修改宣布C对象不是真正的常量。为常量一个更好的名字很可能是只读
- 它真正的意思是,编译器不会让的您的改变。而你需要真正的常数初始化与静态存储(我怀疑regs_to_read
是全球性的)。您可以尝试在其他任何使用该阵列之前被调用函数赋值
regs_to_read
。I am writing some embedded code to interface with an external device over SPI. The device has several registers of varying length and to help keep things straight I have defined the following structure
typedef struct { uint16_t Signed :1; // Register is signed or unsigned uint16_t CommLengthBytes :3; // The width of the register in bytes uint16_t Address :12; // Register address }ts_register;
I have then defined each register in my sources as follows
static const ts_register SAGCYC = {0, 1, 0x000}; static const ts_register DISNOLOAD = {0, 1, 0x001}; static const ts_register LCYCMODE = {0, 1, 0x004}; static const ts_register IRMSA = {0, 4, 0x31A}; static const ts_register IRMSB = {0, 4, 0x31B}; static const ts_register VRMS = {0, 4, 0x31C};
etc.
I have a function that will take a pointer to an array of ts_registers and queue up the SPI transfers required to read all of the registers in the array and call a callback function to handle the reply
My issue comes when I try to make the array of ts_registers that I want to read as follows:
ts_register regs_to_read[3] = {VRMS, IRMSA, IRMSB};
This generates the error: "expression must have a constant value" 3 times (once per array element).
Since they are defined as constants, what have I overlooked?
解决方案Since they are defined as constants, what have I overlooked?
In C objects declared with the
const
modifier aren't true constants. A better name for const would probably bereadonly
- what it really means is that the compiler won't let you change it. And you need true constants to initialize objects with static storage (I suspectregs_to_read
is global).You could try assigning
regs_to_read
in a function called before anything else uses that array.这篇关于C的误差:前pression必须有一个恒定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!