[英]Error when creating an array with memory addresses
I have matrix declaration implemented as follows: 我的矩阵声明实现如下:
int var_porcenComun;
int var_porceninv;
uint32_t pointers[] = {
(uint32_t)&var_porcenComun, 9999999,
(uint32_t)&var_porceninv, 999999
};
Those are all global variables. 这些都是全局变量。 This code compiles fine. 这段代码编译得很好。
Basically, the "pointers" holds the address of a variable and the maximum value it is supposed to hold. 基本上,“指针”保存变量的地址和它应该保持的最大值。
My problem is that I have to add to this matrix a new variable, defined as uint64_t. 我的问题是我必须在这个矩阵中添加一个新变量,定义为uint64_t。 Although the size of a pointer in my platform is 32 bits, I'd have to change the "pointers" to a uint64_t, because of the size of the variable. 虽然我的平台中指针的大小是32位,但由于变量的大小,我必须将“指针”更改为uint64_t。 But when I do: 但当我这样做时:
uint64_t pointers[] = {
(uint64_t)&var_porcenComun, 9999999,
(uint64_t)&var_porceninv, 999999
};
I get the following error: 我收到以下错误:
: Error! E1054: Expression must be constant
Why is this error happening when I simply change the "pointers" type? 当我只是更改“指针”类型时,为什么会发生这种错误?
I'm using watcom 1.3 as compiler. 我使用watcom 1.3作为编译器。 Gcc and Visual Studio 's have compiled fine this code. Gcc和Visual Studio已编译好这段代码。
Globals must be initialised with constant values. 必须使用常量值初始化全局变量。 I guess watcom don't believe that your values are actually constants (maybe it is bad with compile-time calculations). 我想watcom不相信你的值实际上是常量(也许它对于编译时计算是不好的)。
You can try to hack your compiler (like convert it to uint32_t, then automatic conversion may happen; or some bit tricks, none of which is guaranteed) or move array initialisaion out of static (eg moving it somehwere to init
function that you call at the very beginning of main
). 您可以尝试破解您的编译器(例如将其转换为uint32_t,然后可能会发生自动转换;或者某些技巧,无法保证)或将数组初始化移出静态(例如将其移动到您调用的init
函数) main
开始)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.