C++指针类型转换有关问题,求解(无法输出hello)
C++指针类型转换问题,求解(无法输出hello)程序编译通过,运行出错,经过调试错误出现在strcpy((char*)str,
C++指针类型转换问题,求解(无法输出hello)
程序编译通过,运行出错,经过调试错误出现在strcpy((char*)str, "hello");这一句,请高手指教
void fun(char* str)
{
str = (char*)malloc(100);
//strcpy(str, "hello");
}
int _tmain(int argc, _TCHAR* argv[])
{
void *str = NULL;
fun((char*)&str);
strcpy((char*)str, "hello");
cout << str << endl;
return 0;
}
[解决办法]你的行为不一致,程序运行自然就会抱怨了。
[解决办法]void fun(char* str)
{
str = (char*)malloc(100);
//strcpy(str, "hello");
}
这个函数不对,应该是
void fun(char** str)
{
*str = (char*)malloc(100);
//strcpy(str, "hello");
}
看看 林锐 的 高质量C/C++编程指南