我是 C 的新手,我正在尝试编写一个简单的程序来连接两个字符串。在我的 Linux 机器上运行它时,出现以下异常:
test.c:12:10: 错误:“strcat”的类型冲突
你能帮我理解我在这里缺少的东西吗:
#include<stdio.h>
#include<string.h>
void main() {
// string concatenation
char str1[] = {'S', 'h'};
char str2[] = {'X', 'y'};
strcat(str1, str2);
}
void strcat(char str1[], char str2[]) {
int index;
int str1_length = strlen(str1);
for(index=0; index<strlen(str2); index++) {
str1[index + str1_length] = str2[index];
}
}
最佳答案
您的 strcat
没有 standard signature :
char *strcat(char *dest, const char *src);
如果你想包含string.h
,你需要让它匹配。或者,只需为您的实现指定一个不同的名称(例如 my_strcat
)。
您可能还希望以 null 终止您的测试字符串 - 这些 strlen
调用会给您带来非常有趣的结果。此外,为避免写入超过第一个字符串分配内存的末尾(从而导致未定义的行为),请确保为其提供足够的空间以容纳末尾的第二个字符串。
关于c - 这个基本的 strcat 实现有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14152080/