Hey guys im having trouble writing this code for my function. I have the idea down and a way to solve the problem but im having trouble writing it in C code. Basically im writing a function that runs exactly like the function strcat from the string.h library but we cant use it so we have to make our own. We were given a header file and we were suppose to write the functions that were in the header file. This is what i got so you guys can see whats happening:
code for strfuncs.c :
#include <stdio.h>
#include "strfuncs.h"
#define ONE_HUNDRED 100
int strlen(const char *s) {
/* Declarations */
int x=0;
/* Read in characters until null */
while (*s != '\0') {
x++;
s++;
}
return(x);
}
char* strcat(char *to, const char* from) {
/* Declarations */
int length_1,length_2,i;
char *str3;
/* 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;
}
header file:
#ifndef STR_FUNCS_H
#define STR_FUNCS_H
int strlen(const char *str);
char *strcat(char *to, const char *from);
char* modifystr(char *str, char from, char to);
#endif
test file: #include <stdio.h>
#include "strfuncs.h"
#define ONE_HUNDRED 100
int main () {
/* Get user inputs */
char first_choice[ONE_HUNDRED], second_choice[ONE_HUNDRED];
int first_length,second_length;
/* Begin execution */
printf("Enter string one: ");
scanf("%s", first_choice);
printf("Enter string two: ");
scanf("%s", second_choice);
/* Calculate the lengths of the strings and print out */
first_length = strlen(first_choice);
second_length = strlen(second_choice);
printf("String one is %d characters long\n",first_length);
printf("String two is %d characters long\n",second_length);
printf("The concatenated string is: %d", strcat(first_choice,second_choice));
return 0;
}
As you can see i also added the test file to help you understand what im doing. At compile time when i enter the following compile code i get this error message:
gcc -ansi -Wall -Werror -lm strfuncs.c strtester.c
cc1: warnings being treated as errors
strfuncs.c: In function ‘strcat’:
strfuncs.c:41: error: function returns address of local variable
do you guys any suggestions how i could return the arrays contents???