简体   繁体   中英

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. While running it on my Linux box, I get the following exception:

test.c:12:10: error: conflicting types for '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 :

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

You need to make it match if you want to include string.h . Alternately, just give your implementation a different name ( my_strcat , for example).

You probably also will want to null-terminate your test strings - those strlen calls are going to give you pretty interesting results otherwise. 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 ).

You've made your strings this way:

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

So it only has two characters, S and h.

To make a string usable by functions such as strlen , it must have a null-terminator.
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. So its better to rename your strcat function.

There are many errors in your code. First, the strings str1 and str2 should be null terminated. 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> . You can correct this by either renaming your function, or by not including the header string.h . If you do the second one, you should also implement the function strlen , because it is found at 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); . Your user-defined function conflicts with it. As C does not support overloading, you need to rename your function.

Before concatenating, you also have to declare your destination char array so that it has enough space to hold the concatenated string. 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. 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).

Your function can't have that name since it is declared in string.h. Simply rename your function to myStrcat or something of that nature.

Replace strcat with str_cat , because your function name is conflicting with function already present in 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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