初始化变量并在同一时间指定存储地址:可以吗? [英] Initializing a variable and specifying the storage address the same time: is it possible?
问题描述
在 Atmel 处理器的 codevision 编译器中,可以指定全局变量的存储地址,例如
In the codevision compiler for Atmel processors, there is a possibility to specify the storage address of a global variable, for example
int a @0x100; // will place the variable at the address 0x100 in RAM
当然,根据标准C,变量可以在声明时初始化
Of course, as per standard C, variables can be initialized upon declaration
int a=42;
但是,我没有发现任何可能性。 int a @ 0x100 = 42
或 int a = 42 @ 0x100;
不工作,它们会导致编译器错误。
However, I did not find any possibility to do them both. int a @0x100 = 42
or int a = 42 @0x100;
don't work, they cause compiler errors.
您可能会问为什么这么重要,因为可以简单地使用
You might ask why it is so important to do it, because one could simply have
int a @0x100;
int main()
{
a = 42;
//...
}
但是,如果我有变量 EEPROM ,我需要初始化它们,因为这是自动生成带有值的eeprom文件的唯一方法。我不能稍后再分配这些值,因为在这种情况下,它将在程序的每个开始时将值写入eeprom。
However, if I have variables in the EEPROM, I need to initialize them, because this is the only way to automatically generate the eeprom file with the values in it. I can't assign those values later, because in that case it would actually write the values into the eeprom at each start of the program.
推荐答案
看看 CodeVisionAVR
帮助,
如果一个全局变量,必须使用以下过程使用 @ operator
放在特定地址时,必须在声明期间初始化:
"The following procedure must be used if a global variable, placed at a specific address using the @ operator
, must be initialized during declaration:
/* the variable will be stored in EEPROM at address 0x10 */
eeprom int abc @0x10;
/* and it will be initialized with the value 123 */
eeprom int abc=123;
这篇关于初始化变量并在同一时间指定存储地址:可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!