简体   繁体   English

创建具有内存地址的数组时出错

[英]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. GccVisual 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.

相关问题 C:memory 地址数组? - C: Array of memory addresses? 在C中为结构中的数组分配顺序存储器地址 - Allocating sequential memory addresses for array in structure in C 当我指向一个数组的指针时。 为什么我指向的数组的指针和索引[0] 有不同的 memory 地址? - When I point a pointer to an array. Why does the pointer and the index[0] of the array I am pointing to have different memory addresses? 在哪里可以释放 memory,同时创建二维数组? valgrind 错误 - Where to free memory, while creating 2d array? valgrind error 了解C中数组元素中的已分配内存地址(Windows 10中为gcc) - Understanding allocated memory addresses in array elements in C (gcc in Windows 10) 从数组中获取仅C中某些值的内存地址 - Getting memory addresses from array for only some values in C 可以在阵列的索引之间交换内存地址吗? - Exchange Memory addresses between array's indixes possible? 在C中给出开始和结束内存地址的数组遍历的意外行为 - Unexpected behavior traversing array given beginning and ending memory addresses in C 快速排序:打印内存地址而不是数组元素: - QUICK SORT: Printing Memory Addresses instead of array elements: 内存地址如何知道数组的长度? - How do memory addresses know how long an an array is?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM