【发布时间】:2021-03-07 07:38:48
【问题描述】:
我是 C++ 新手。据我所知,上述情况names[0] 应该是'R' 的索引。
我想我错过了或缺乏知识。
请帮帮我。
#include<iostream>
using namespace std;
int main() {
char *names[] = {
"Rohan",
"Sammy",
"Samuel",
"Henil"
};
// Expected R to be printed and not Rohan
cout << *names << endl;
return 0;
}
输出:
Rohan
【问题讨论】:
-
names是一个数组。所以,在*namesnames已经衰减为一个指针。因此,它的类型为char**。 -
仅供参考,您的代码无法在 C++11 及更高版本中编译,因为您无法将字符串文字分配给非常量
char*。改用const char*:const char* names[] = {"Rohan", ...};
标签: c++ c string pointers char