字符串常量的地址

发布于 2024-11-06 11:16:57 字数 293 浏览 1 评论 0原文

我从汇编器到 C 中获取字符串的地址,并且我需要获取该地址的内容。 怎么做呢?谷歌提供了带有reinterpret_cast的C++示例,但它在C中不起作用(我想)。 如果您也注意到所需的库,我将不胜感激,tenx

#include <stdio.h> 
#include <stdlib.h> 

unsigned long const1(void); 

int main()
{ 
    printf("Const: %d\n", const1()); 
    return 0; 
}

I get an addres of string from assembler into C, and I need to get content of this address.
How to do it? Google gives C++ examples with reinterpret_cast, but it not working in C (I suppose).
I will appreciate if you will note needed libs too, tenx

#include <stdio.h> 
#include <stdlib.h> 

unsigned long const1(void); 

int main()
{ 
    printf("Const: %d\n", const1()); 
    return 0; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

忆依然 2024-11-13 11:16:57

如果您已经获得了地址并且知道它是一个以空字符结尾的字符串,那么您所需要做的就是将其视为字符串。

printf("%s", (char*)alleged_string_address);

If you've already got the address and you know it's a null terminated string, then all you need to do is treat it like a string.

printf("%s", (char*)alleged_string_address);
勿忘心安 2024-11-13 11:16:57

在等待您的信息时的初步答案:

   char* foo = (char*)...pointer from assembly...;
    *foo = 'a'; /* write a to the address pointed at by foo */
    foo++;      /* increment the address of foo by 1 */
    *foo = 'b'; /* write b to that address.  foo now contains ab, if it points at RAM.  */

此答案适用于嵌入式系统。如果您需要一个指向外围寄存器之类的指针,请使用 volatile 来避免编译器优化。

   volatile char* foo = (char*)...pointer from assembly...;
    *foo = 'a'; /* write a to the address pointed at by foo */
    foo++;      /* increment the address of foo by 1 */
    *foo = 'b'; /* write b to that address.  foo now contains ab, if it points at RAM.  */

Preliminary answer while I wait for your info:

   char* foo = (char*)...pointer from assembly...;
    *foo = 'a'; /* write a to the address pointed at by foo */
    foo++;      /* increment the address of foo by 1 */
    *foo = 'b'; /* write b to that address.  foo now contains ab, if it points at RAM.  */

This answer is geared toward embedded systems. If you need a pointer to something like a peripheral register, use volatile to avoid compiler optimizations.

   volatile char* foo = (char*)...pointer from assembly...;
    *foo = 'a'; /* write a to the address pointed at by foo */
    foo++;      /* increment the address of foo by 1 */
    *foo = 'b'; /* write b to that address.  foo now contains ab, if it points at RAM.  */
就像说晚安 2024-11-13 11:16:57

如果您知道字符串后面有一个带零的字节,请尝试以下操作:

char* p = (char*) <your address here>;
// use p for whatever here

如果字符串后面没有零,则 C 中的标准字符串函数将失败。

If you know there's a byte with zero in it after the string then try this:

char* p = (char*) <your address here>;
// use p for whatever here

If there's no zero following the string then the standard string functions in C will fail.

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