字符串C ++中指针的输出位置

时间:2016-11-30 04:32:48

标签: c++ string pointers output

我现在下面的代码输出位于0位置的字母H,但我想知道是否还有一种方法不仅输出字母H而且输出字母H的指针位置。

谢谢

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main(){
const char* letterPointer = "Harry"; 
cout << letterPointer[0] << endl
return 0;
}

1 个答案:

答案 0 :(得分:1)

为此目的,从void *转换为char *是一种常用技巧,如下所示:

cout << static_cast<const void*>(&letterPointer[0]) << endl;

顺便说一句,在C ++中,string并不意味着char *,而是std::string。 因此,代码中不需要包含该行。

#include <string> // You'd probably want to remove this line.