Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey you guys im writing the strcat function that resembles that ofthe strcat fun

ID: 3618729 • Letter: H

Question

Hey you guys im writing the strcat function that resembles that ofthe strcat function in the string.h. Our professor does not want usto use strcat function from the string.h library so we have to makeour own function. So I wrote the code and everything but at compiletime (gcc -ansi -Wall) i get this error: cc1: warnings being treated as errors strfuncs.c: In function ‘strcat’: strfuncs.c:41: error: function returns address of localvariable
I wrote the code below so you guys can see what i have so far.What shoul i change from my code so i dont get the errormessage???
char* strcat(char *to, const char* from) {   /* Declarations */   int length_1,length_2,i;   char str3[ONE_HUNDRED];      /* Get the length of each string */   length_1 = strlen(to);   length_2 = strlen(from);      for (i=0;i < length_1;i++) {    str3[i] = to[i];   }      for (i=0;i < length_2;i++) {    str3[length_1 + i] = from[i];   }      return str3; } cc1: warnings being treated as errors strfuncs.c: In function ‘strcat’: strfuncs.c:41: error: function returns address of localvariable
I wrote the code below so you guys can see what i have so far.What shoul i change from my code so i dont get the errormessage???
char* strcat(char *to, const char* from) {   /* Declarations */   int length_1,length_2,i;   char str3[ONE_HUNDRED];      /* Get the length of each string */   length_1 = strlen(to);   length_2 = strlen(from);      for (i=0;i < length_1;i++) {    str3[i] = to[i];   }      for (i=0;i < length_2;i++) {    str3[length_1 + i] = from[i];   }      return str3; } cc1: warnings being treated as errors strfuncs.c: In function ‘strcat’: strfuncs.c:41: error: function returns address of localvariable
I wrote the code below so you guys can see what i have so far.What shoul i change from my code so i dont get the errormessage???
char* strcat(char *to, const char* from) {   /* Declarations */   int length_1,length_2,i;   char str3[ONE_HUNDRED];      /* Get the length of each string */   length_1 = strlen(to);   length_2 = strlen(from);      for (i=0;i < length_1;i++) {    str3[i] = to[i];   }      for (i=0;i < length_2;i++) {    str3[length_1 + i] = from[i];   }      return str3; } char* strcat(char *to, const char* from) {   /* Declarations */   int length_1,length_2,i;   char str3[ONE_HUNDRED];      /* Get the length of each string */   length_1 = strlen(to);   length_2 = strlen(from);      for (i=0;i < length_1;i++) {    str3[i] = to[i];   }      for (i=0;i < length_2;i++) {    str3[length_1 + i] = from[i];   }      return str3; }

Explanation / Answer

char* strcat(char *to, const char* from) {   /* Declarations */   int length_1,length_2,i;   char* str3= new char[100] ;      /* Get the length of each string */   length_1 = strlen(to);   length_2 = strlen(from);      for (i=0;i < length_1;i++) {     *(str3+i) =*( to+i);   }      for (i=0;i < length_2;i++) {    * (str3+length_1 + i) =*( from+i);   }      return str3; }

or in your code you can return *str3
because your funtion its char* type and your str3 its chartype
char* strcat(char *to, const char* from) {   /* Declarations */   int length_1,length_2,i;   char* str3= new char[100] ;      /* Get the length of each string */   length_1 = strlen(to);   length_2 = strlen(from);      for (i=0;i < length_1;i++) {     *(str3+i) =*( to+i);   }      for (i=0;i < length_2;i++) {    * (str3+length_1 + i) =*( from+i);   }      return str3; }

or in your code you can return *str3
because your funtion its char* type and your str3 its chartype