简体   繁体   English

这个基本的strcat实现有什么问题?

[英]What's wrong with this basic strcat implementation?

I am fairly new to C and I am trying to write a simple program to concatenate two strings. 我对C相当陌生,我正在尝试编写一个简单的程序来连接两个字符串。 While running it on my Linux box, I get the following exception: 在Linux机器上运行它时,出现以下异常:

test.c:12:10: error: conflicting types for 'strcat' test.c:12:10:错误:“ strcat”的类型冲突

Can you please help me understand what I am missing here: 您能帮我了解我在这里缺少什么吗?

#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];
  }
}

Your strcat doesn't have the standard signature : 您的strcat没有标准签名

char *strcat(char *dest, const char *src);

You need to make it match if you want to include string.h . 如果要包含string.h则需要使其匹配。 Alternately, just give your implementation a different name ( my_strcat , for example). 或者,只需为您的实现提供一个不同的名称( my_strcatmy_strcat )。

You probably also will want to null-terminate your test strings - those strlen calls are going to give you pretty interesting results otherwise. 您可能还想对测试字符串进行空终止-否则,这些strlen调用将给您带来非常有趣的结果。 In addition, to avoid writing past the end of your first string's allotted memory (and thereby causing undefined behaviour), make sure you give it enough space to fit the second string on the end. 此外,为避免写入超出第一个字符串分配的内存末尾(从而导致未定义的行为),请确保为它留出足够的空间以适合第二个字符串末尾。

Aside from what others have said about the name conflict, you also don't have proper strings. 除了其他人关于名称冲突的说法之外,您也没有适当的字符串。

In C, strings end with a null-terminator ( \\0 ). 在C中,字符串以空终止符( \\0 )结尾。

You've made your strings this way: 您已经按照以下方式制作了字符串:

char str1[] = {'S', 'h'};

So it only has two characters, S and h. 因此它只有两个字符,S和h。

To make a string usable by functions such as strlen , it must have a null-terminator. 要使字符串可用于诸如strlen的函数,它必须具有一个空终止符。
I recommend: 我建议:

char str1[] = {'S', 'h', '\0'};

there is predefined function "strcat" in C language(in string.h) so when you are trying to call strcat then compiler seems conflict between that inbuild function and your defined function. C语言中有预定义的函数“ strcat”(在string.h中),因此当您尝试调用strcat时,编译器似乎在该inbuild函数和您定义的函数之间发生冲突。 So its better to rename your strcat function. 因此最好重命名您的strcat函数。

There are many errors in your code. 您的代码中有很多错误。 First, the strings str1 and str2 should be null terminated. 首先,字符串str1和str2应该以null终止。 You can correct that by changing their declarations to 您可以通过将其声明更改为

char str1[] = "Sh";
char str2[] = "Xy";

or to 或者

char str1[] = {'S', 'h', '\0'};
char str2[] = {'X', 'y', '\0'};

And secondly, strcat is a reserved name, so your main function doesn't call your version, but the version that is found at the header file <string.h> . 其次, strcat是保留名称,因此您的主函数不会调用您的版本,而是在头文件<string.h>找到的版本。 You can correct this by either renaming your function, or by not including the header string.h . 您可以通过重命名函数或不包含标头string.h来更正此问题。 If you do the second one, you should also implement the function strlen , because it is found at string.h . 如果执行第二个操作,则还应该实现strlen函数,因为该函数位于string.h

string.h已经提供了strcat因此只需将函数重命名为其他名称即可。

There is already a strcat function defined in the string.h library, with the signature char *strcat(char *Destination, char *Source); string.h库中已经定义了一个strcat函数,其签名为char *strcat(char *Destination, char *Source); . Your user-defined function conflicts with it. 您的用户定义函数与此冲突。 As C does not support overloading, you need to rename your function. 由于C不支持重载,因此您需要重命名函数。

Before concatenating, you also have to declare your destination char array so that it has enough space to hold the concatenated string. 串联之前,还必须声明目标char数组,以便它有足够的空间容纳串联的字符串。 If not, you will run over the array bound, which may cause strange problems. 如果没有,您将在数组边界上运行,这可能会导致奇怪的问题。

Also, your strings should be null terminated, because the standard library relies on that to determine where the string ends. 另外,您的字符串应以null终止,因为标准库依赖于此来确定字符串的结尾。 Without the null terminator, the strlen cannot figure out the string length (unless by accident the next memory location happens to have a NULL character - you should never rely on such accidents). 没有空终止符, strlen无法计算出字符串的长度(除非偶然,下一个存储位置碰巧有一个NULL字符-您永远不要依赖此类意外)。

Your function can't have that name since it is declared in string.h. 您的函数不能使用该名称,因为它是在string.h中声明的。 Simply rename your function to myStrcat or something of that nature. 只需将函数重命名为myStrcat或类似的名称。

Replace strcat with str_cat , because your function name is conflicting with function already present in string.h strcat替换为str_cat ,因为您的函数名与string.h已经存在的函数冲突

#include<stdio.h>
#include<string.h>

void main() {
  // string concatenation
  char str1[] = {'S', 'h'};
  char str2[] = {'X', 'y'};
  str_cat(str1, str2);
}

void str_cat(char str1[], char str2[]) {
  int index;
  int str1_length = strlen(str1);
  for(index=0; index<strlen(str2); index++) {
    str1[index + str1_length] = str2[index];
  }
}

Just change your code as below: 只需如下更改代码:

 replace char str1[] = {'S', 'h'} by char str1[] = {'S', 'h', '\0'};
 replace char str2[] = {'X', 'y'} by char str2[] = {'X', 'y', '\0'};
#include<stdio.h>
#include<string.h>

void mystrcat(char str1[], char str2[]);

void main() {
char str1[] = {'S', 'h' , '\0'};
char str2[] = {'X', 'y', '\0'};
mystrcat(str1, str2);
}

// string concatenation
void mystrcat(char str1[], char str2[]) {
int index;
int str1_length = strlen(str1);
for(index=0; index<strlen(str2); index++) {
    str1[index + str1_length] = str2[index];
}
str1[index + str1_length] = '\0';
printf("%s\n",str1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM