显示字符串的地址 [英] Displaying the address of a string

查看:84
本文介绍了显示字符串的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

char* hello = "Hello World";
std::cout << "Pointer value = " << hello << std::endl;
std::cout << "Pointer address = " << &hello << std::endl;

结果如下:

Pointer value = Hello World
Pointer address = 0012FF74

当我使用OllyDbg调试程序时,看到0x0012FF74的值是0x00412374.

When I debug to my program using OllyDbg, I see that the value of 0x0012FF74 is e.g. 0x00412374.

有什么办法可以打印hello指向的实际地址?

Is there any way I can print the actual address that hello points to?

推荐答案

如果使用&hello,它将打印指针的地址,而不是字符串的地址.将指针强制转换为void*以使用operator<<的正确重载.

If you use &hello it prints the address of the pointer, not the address of the string. Cast the pointer to a void* to use the correct overload of operator<<.

std::cout << "String address = " << static_cast<void*>(hello) << std::endl;

这篇关于显示字符串的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆