C语言中的 @ 符号是什么意思?
Global Variable Address Modifier (@address)
You can assign global variables to specific addresses with the global variable address modifier. These variables are called 'absolute variables'. They are useful for accessing memory mapped I/O ports and have the following syntax:
Declaration = <TypeSpec> <Declarator>[@<Address>|@"<Section>"]
[= <Initializer>];
<TypeSpec> is the type specifier, e.g., int, char
<Declarator> is the identifier of the global object, e.g., i, glob
<Address> is the absolute address of the object, e.g., 0xff04, 0x00+8
<Initializer> is the value to which the global variable is initialized.
A segment is created for each global object specified with an absolute address. This address must not be inside any address range in the SECTIONS entries of the link parameter file. Otherwise, there would be a linker error (overlapping segments). If the specified address has a size greater than that used for addressing the default data page, pointers pointing to this global variable must be "__far". An alternate way to assign global variables to specific addresses is (Listing 8.8).
Listing 8.8 Assigning global variables to specific addresses
#pragma DATA_SEG [__SHORT_SEG] <segment_name>
setting the PLACEMENT section in the linker parameter file. An older method of accomplishing this is shown in Listing 8.9.
Listing 8.9 Another means of assigning global variables to specific addresses
<segment_name> INTO READ_ONLY <Address> ;
Listing 8.10 is a correct and incorrect example of using the global variable address modifier and Listing 8.11 is a possible PRM file that corresponds with example Listing.
Listing 8.10 Using the global variable address modifier
//看这意思,就是把int型变量glob地址从0x0500开始,并把值10初始化时放在0x0500
int glob @0x0500 = 10; // OK, global variable "glob" is
// at 0x0500, initialized with 10
void g() @0x40c0; // error (the object is a function)
void f() {
int i @0x40cc; // error (the object is a local variable)
}
全局变量地址修饰符(@address)
您可以使用全局变量地址修饰符将全局变量分配给特定地址。这些变量称为“绝对变量”。它们对于访问内存映射的I / O端口很有用,并具有以下语法:
声明= <TypeSpec> <声明符> [@ <地址> | @“ <部分>”]
[= <Initializer>];
<TypeSpec>是类型说明符,例如int,char
<Declarator>是全局对象的标识符,例如i,glob
<Address>是对象的绝对地址,例如0xff04、0x00 + 8
<Initializer>是全局变量初始化到的值。
将为每个用绝对地址指定的全局对象创建一个段。该地址不得在链接参数文件的SECTIONS条目中的任何地址范围内。否则,将出现链接器错误(重叠段)。如果指定的地址的大小大于用于寻址默认数据页的大小,则指向此全局变量的指针必须为“ __far”。将全局变量分配给特定地址的另一种方法是(清单8.8)。
清单8.8将全局变量分配给特定地址
#pragma DATA_SEG [__SHORT_SEG] <段名称>
在链接器参数文件中设置PLACEMENT部分。清单8.9显示了完成此操作的较旧方法。
清单8.9将全局变量分配给特定地址的另一种方法
<段名称> INTO READ_ONLY <地址>;
清单8.10是使用全局变量地址修饰符的正确和不正确的示例,清单8.11是与示例清单相对应的可能的PRM文件。
清单8.10使用全局变量地址修饰符
//看这意味着,就是把int型变量glob地址从0x0500开始,并把值10初始化时放在0x0500
int glob @ 0x0500 = 10; //确定,全局变量“ glob”为
//在0x0500处,以10初始化
无效g()@ 0x40c0; //错误(对象是一个函数)
无效f(){
int我@ 0x40cc; //错误(对象是局部变量)
}
主要在嵌入式内应用多,将某一变量名称指向寄存器的地址处,之后对此地址处寄存器的操作(赋值)只需要对此变量名操作即可
以pic16F877A中pic16F877.h头文件对寄存器地址的命名举例
static volatile unsigned char INDF @0x00;
static volatile unsigned char TMR0 @0x01;
这里将0x00地址处赋予INDF,而INDF对应多少位呢,unsigned char限定INDF表示8位,这样就对0x00物理地址下的8位寄存器命名了一个变量,之后操作此地址下寄存器只需要操作此变量即可,不需要在记忆寄存器的地址是什么,只需要记住它对应的变量名即可,毕竟记名字要比一系列数字要好的多;
还可以使用指针符号来对某一地址下寄存器进行命名举例说明
以上面情况举例
#define INDF (static volatile unsigned char *)0x00;
将0x00强制类型转换为unsigned char 的指针类型 并对它赋予另外一个名字INDF
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗