【发布时间】:2017-11-23 20:52:06
【问题描述】:
我开始学习如何用 C++ 编写代码。 我一直在阅读 c++ 入门加(第 5 版)一书,遇到了一个我不完全理解的示例程序。基本上,该程序会询问您的姓氏并为您提供存储位置的地址:
#include <iostream>
#include <cstring>
using namespace std;
char* getname();
int main();
{
char* name;
name = getname();
cout << name << " at " << (int*)name << endl;
delete [] name;
name = getname();
cout << name << " at " << (int*)name << endl;
delete [] name;
return 0;
}
char* getname()
{
char temp[80];
cout << "Enter last name: ";
cin >> temp;
char* pn = new char [strlen(temp)+1];
strcpy(pn, temp);
return pn;
}
我不太明白为什么 char* getname() 函数需要解引用运算符。我在整体上理解这个程序有点麻烦,呵呵。 抱歉,如果这是一个愚蠢的问题,但我很困惑。就这样。谢谢!..
【问题讨论】:
-
欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。
-
哪个解除引用操作符?长期没有解除引用。
-
它不是
*getname(),返回类型为char。相反,它是getname(),返回类型为char*。谷歌“C 类型声明”了解更多信息(第一个结果还不错)。 -
你可能应该参考解释 C 中指针的章节...
-
@ArthurTacca 这帮了大忙!现在一切都清楚了,谢谢你,伙计。
标签: c++ function dereference