gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:24:14 24 4
gpt4 key购买 nike

我现在下面的代码输出位置 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;
}

最佳答案

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

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

顺便说一下,在 C++ 中,string 并不意味着 char *,而是 std::string .因此,此包含行在您的代码中不是必需的。

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

关于c++ - 指针在字符串C++中的输出位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40880363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com