gpt4 book ai didi

C++:指针输出让我困惑

转载 作者:行者123 更新时间:2023-11-30 01:27:22 24 4
gpt4 key购买 nike

所以我有以下代码:

    cout << _userLoginName << endl;
cout << *_userLoginName << endl;
cout << (_userLoginName+1) << endl;
cout << *(_userLoginName+1) << endl;

变量 char * _userLoginName 已设置为等于 "smith"。我的问题很简单:为什么在最后几行代码中会得到以下输出?

smith // as from cout << _userLoginName << endl;
s // as from cout << *_userLoginName << endl;
mith // cout << (_userLoginName+1) << endl;
m // cout << *(_userLoginName+1) << endl;

我确实尝试对结果进行推理,但我无法弄清楚。谢谢。

最佳答案

如果你给cout 1一个char * ,它将尝试打印一个字符串。如果你给它一个char , 然后它将打印该单个字符。

_userLoginName(_userLoginName+1)类型为 char * ; *_userLoginName*(_userLoginName+1)类型为 char .


<子>1。从技术上讲,“给出 std::operator<<(std::ostream &, T)”。

关于C++:指针输出让我困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9088934/

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