内存地址字面量
给定十六进制格式的文字内存地址,如何在 C 中创建一个寻址该内存位置的指针?
我的平台(IBM iSeries)上的内存地址是128位。 C类型long long
也是128位。
想象一下,我有一个字符串(字符数组)的内存地址,即:C622D0129B0129F0
我假设正确的 C 语法可以直接寻址该内存位置:
const char* const p = (const char* const)0xC622D0129B0129F0ULL
我使用 ULL
后缀表示无符号 long long 文字。
我的内核/平台/操作系统是否允许我这样做是另一个问题。我首先想知道我的语法是否正确。
Given a literal memory address in hexadecimal format, how can I create a pointer in C that addresses this memory location?
Memory addresses on my platform (IBM iSeries) are 128bits. C type long long
is also 128bits.
Imagine I have a memory address to a string (char array) that is: C622D0129B0129F0
I assume the correct C syntax to directly address this memory location:
const char* const p = (const char* const)0xC622D0129B0129F0ULL
I use ULL
suffix indicate unsigned long long literal.
Whether my kernel/platform/operating system will allow me to do this is a different question. I first want to know if my syntax is correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的语法几乎是正确的。您不需要这些
const
之一:p
之前的const
表示变量p
初始化后无法更改。它不涉及p
所指向的任何内容,因此您不需要将它放在右侧。Your syntax is almost correct. You don't need one of those
const
:The
const
immediately beforep
indicates that the variablep
cannot be changed after initialisation. It doesn't refer to anything about whatp
points to, so you don't need it on the right hand side.C 中不存在像地址文字这样的东西。
唯一保证在整数和指针之间工作的东西是从
void*
转换为uintptr_t
(如果存在)然后回来。如果您从某处以整数形式获取地址,则该地址应为uintptr_t
类型。使用类似的东西将其转换回来。
您必须包含
stdint.h
才能获取类型uintptr_t
和宏UINTMAX_C
。There is no such thing like an address literal in C.
The only thing that is guaranteed to work between integers and pointers is cast from
void*
touintptr_t
(if it exists) and then back. If you got your address from somewhere as an integer this should be of typeuintptr_t
. Use something liketo convert it back.
You'd have to include
stdint.h
to get the typeuintptr_t
and the macroUINTMAX_C
.