我在与改变字符指针的一些问题,并不能找出我要去哪里错了问题与改变一个const char指针
这里是我的改变说明功能...
void appointment::changeDescription(const char * s) // change an existing description
{
if (desc != NULL)
strcpy(desc, s);
if (s == NULL)
return;
}
这里是调用变更描述函数的函数。
bool keyBoardEnterAppointment(schedule & sched) // return true if successful
// return false if full
{
if (sched.isFull() == true)
{
cout << "Schedule is FULL." << endl;
return false;
}
else
{
appointment s;
int day, month, year;
char *desc = new char;
long source;
cout << "*/Enter Appointment\\* ";
cout << "Description: "; cin >> desc;
cout << "Source: "; cin >> source;
cout << "Month: "; cin >> month;
cout << "Day: "; cin >> day;
cout << "Year: "; cin >> year;
s.changeDescription(desc);
s.setSource(source);
s.setDay(day);
s.setMonth(month);
s.setYear(year);
sched.addtoSchedule(s);
sched.print(cout);
return true;
}
}
它编译和运行,但说明仍然是一样的默认构造函数说明...
让自己的生活更轻松,只需使用'std :: string'。 – shuttle87 2014-11-24 00:47:58
'char * desc = new char;'这只为一个字母分配足够的内存。也许尝试:'char * desc = new char [32]; // 31个字母(终止符+1)'。或者char'desc [32];'那么你不必'删除'它(你在这里没有做的)。 – Galik 2014-11-24 00:48:23
不应该先检查's'是否为空? – 2014-11-24 01:00:16