内存地址字面量

发布于 2024-09-16 13:30:11 字数 399 浏览 2 评论 0原文

给定十六进制格式的文字内存地址,如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-09-23 13:30:11

你的语法几乎是正确的。您不需要这些 const 之一:

const char* const p = (const char*)0xC622D0129B0129F0ULL

p 之前的 const 表示变量 p 初始化后无法更改。它不涉及 p 所指向的任何内容,因此您不需要将它放在右侧。

Your syntax is almost correct. You don't need one of those const:

const char* const p = (const char*)0xC622D0129B0129F0ULL

The const immediately before p indicates that the variable p cannot be changed after initialisation. It doesn't refer to anything about what p points to, so you don't need it on the right hand side.

爺獨霸怡葒院 2024-09-23 13:30:11

C 中不存在像地址文字这样的东西。

唯一保证在整数和指针之间工作的东西是从 void* 转换为 uintptr_t (如果存在)然后回来。如果您从某处以整数形式获取地址,则该地址应为 uintptr_t 类型。使用类似的东西

(void*)(uintptr_t)UINTMAX_C(0xC622D0129B0129F0)

将其转换回来。

您必须包含 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* to uintptr_t (if it exists) and then back. If you got your address from somewhere as an integer this should be of type uintptr_t. Use something like

(void*)(uintptr_t)UINTMAX_C(0xC622D0129B0129F0)

to convert it back.

You'd have to include stdint.h to get the type uintptr_t and the macro UINTMAX_C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文