我不明白这行是做什么的:
((struct Example*) 0x10000)
Run Code Online (Sandbox Code Playgroud)
我写了一个测试程序:
#include <stdio.h>
struct Elf{
int bla;
char bla2;
};
int main(){
struct Elf *elfPtr;
printf("Before casting: %p\n", elfPtr);
elfPtr = ((struct Elf *)0x10000);
printf("After casting: %p\n", elfPtr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
在投射之前:0xb776dff4
投射后:0x10000
这条线只做这个吗?
elfPtr = 0x10000
Run Code Online (Sandbox Code Playgroud)
这会将指针设置为特定的常量值,这在具有虚拟内存管理的系统中是个坏主意.
一个值得注意的例外是具有内存映射资源的嵌入式系统.在这样的系统中,硬件设计者经常保留一系列存储器地址以供替代使用,使程序员能够访问硬件寄存器,就好像它们是常规存储空间的一部分一样.
这是一个例子*:
struct UART {
char data;
char status;
};
const UART *uart1 = 0xC0000;
const UART *uart2 = 0xC0020;
Run Code Online (Sandbox Code Playgroud)
通过这种设置,嵌入式程序可以访问UART的寄存器,就好像它们是struct成员一样:
while (uart1->status & 0x02) {
uart1->data = 0x00;
}
Run Code Online (Sandbox Code Playgroud)