C语言 如何通过C代码从绝对地址读取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18741219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a value from an absolute address through C code
提问by niteshnarayanlal
I wanted to read a value which is stored at an address whose absolute value is known. I am wondering how could I achieve this. For example. If a value is stored at 0xff73000. Then is it possible to fetch the value stored here through the C code. Thanks in advance
我想读取一个存储在绝对值已知的地址处的值。我想知道我怎么能做到这一点。例如。如果值存储在 0xff73000。那么是否有可能通过C代码获取这里存储的值。提前致谢
回答by Juraj Blaho
Just assign the address to a pointer:
只需将地址分配给一个指针:
char *p = 0xff73000;
And access the value as you wish:
并根据需要访问该值:
char first_byte = p[0];
char second_byte = p[1];
But note that the behavior is platform dependent. I assume that this is for some kind of low level embedded programming, where platform dependency is not an issue.
但请注意,该行为取决于平台。我认为这是针对某种低级嵌入式编程,其中平台依赖性不是问题。
回答by Zdeněk Gromnica
Two ways:
两种方式:
1. Cast the address literal as a pointer:
1. 将地址文字转换为指针:
char value = *(char*)0xff73000;
2. Assign the address to a pointer:
2. 将地址分配给指针:
char* pointer = 0xff73000;
Then access the value:
然后访问该值:
char value = *pointer;
char fist_byte = pointer[0];
char second_byte = pointer[1];
Where charis the type your address represents.
char您的地址代表的类型在哪里。
回答by Ari
char* p = 0x66FC9C;
This would cause this error :
这将导致此错误:
Test.c: In function 'main': Test.c:57:14: warning: initialization makes pointer from integer without a cast [-Wint-conversion] char* p = 0x66FC9C;
Test.c:在函数“main”中:Test.c:57:14:警告:初始化使指针从整数而不进行强制转换 [-Wint-conversion] char* p = 0x66FC9C;
To set a certain address you'd have to do :
要设置某个地址,您必须执行以下操作:
char* p = (char *) 0x66FC9C;

