c - strcat() 无法连接

标签 c arrays string strcat

我有一个二维字符数组要连接成一个数组。它有一个错误:

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/

相关文章:

javascript - 在 JavaScript 中比较字符串的最佳方法?

c# - 从字符串中删除下划线,除了字符串中的链接

c - C语言中的"too many arguments to function"

java - 如何使用形状的面积从该数组中删除形状?

c++ - 打印出结构数组的元素

C 中的字符串数组

c - 围绕原点旋转三角形

c - getline的二次使用

c++ - 使用 void 指针从 c 函数调用模板化成员

javascript - 获得第一个单词在位置 0 和其他单词在位置 1 的数组作为输出的最佳方法?