-1
我开始如何在C++中编写代码。 我一直在阅读C++ primer plus(第5版)书,并且遇到了一个我不完全了解的示例程序。基本上,程序要求你的姓,给你的会储存在地址:这个功能是如何工作的? char * getname(); C++
#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;
}
我不太明白为什么字符*的getName()函数需要引用操作。我在整体上理解这个程序时有点麻烦,嘿嘿。 很抱歉,如果这看起来像一个愚蠢的问题,但我很困难。就这样。谢谢!
欢迎来到Stack Overflow。请花些时间阅读[The Tour](http://stackoverflow.com/tour),并参阅[帮助中心](http://stackoverflow.com/help/asking)中的资料,了解您可以在这里问。 –
哪_derefeencing operator_?没有很长时间没有取消引用。 –
它不是返回类型为char的'* getname()'。相反,它是'getname()',返回类型为'char *'。谷歌“C型声明”获取更多信息(第一个结果非常好)。 –