我有一个二维字符数组要连接成一个数组。它有一个错误:
error C2664: 'strcat' : cannot convert parameter 1 from 'char *[80]' to 'char *'
代码如下:
char *article[5] = {"the", "a", "one", "some", "any"};
char *sentence[80];
num = rand() % 5;
for(int x = 0; x < strlen(article[num]); x++){
strcat(sentence, article[num][x]); //a random element will be concatinated to the sentence array
}
最佳答案
这里有一些固定的代码可能会做你想做的事,但很难确定你想要什么......
srand(time_t(0)); // seed the random number generate once
// use const when handling pointers to string literals
const char* article[5] = {"the", "a", "one", "some", "any"};
char sentence[80]; // 80 character buffer in automatic storage - no pointers
sentence[0] = '\0'; // empty ASCIIZ string to start
for (int x = 0; x < 5; ++x)
{
int num = rand() % 5;
strcat(sentence, article[num]);
}
printf("%s\n", sentence);
关于c - strcat() 无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30229460/