【发布时间】:2013-11-11 20:17:05
【问题描述】:
我有这样的函数,它获取 2 个数组作为 char 参数,我需要合并这些字符串。抱歉,这个问题很简单。
void Abc (char **a, char **b)
{
for (int i = 0; i < 200; i++)
{
a[i]="hello ";
}
for (int i = 0; i < 200; i++)
{
strcat(a[i], b[i]); //doesn't show mistake, but it's there
cout << b[i]<<'\n'; //ok
cout << a[i]<<'\n'; //ok
cout<<strlen(b[i])<<'\n'; //ok
cout<<strlen(a[i])<<'\n'; //ok
}
}
为什么 Strcat 在这里不起作用?我可以做些什么来完成这项工作?
【问题讨论】:
-
strncat()需要三个参数。 -
如果我使用 strcat 它也不起作用
-
您不能修改字符串文字。将指针指向字符串文字
const char *是一个好主意,并且是 C++11 标准所要求的。 -
如果 'a' 指向可以保存你的字符串 "hello" 和 strcat 之后的结果字符串的东西,你可以通过在你的第一个循环中重新分配给 "hello" 来有效地破坏这种可能性。
标签: c++ arrays pointers char strcat