(EDIT)
Probable misunderstanding in updating and returning a string from a function in C.
I'm a student and have just started trying out dynamic memory allocation techniques. Recently, I was told to work on a string copy() function similar to the strcpy() function in C under <string.h>.
It is supposed to be taking two arguments: char c1[],char c2[]. Momentarily, I am focusing on a constraint wherein the length of string c1 is lesser than the string c2. This is the case, we will be copying a larger string into a smaller one. So, naturally it would have to expand the former string's size. (I have defined a function len() which returns the length of the string. It's working fine)
Code:
char* copy(char c1[],char c2[])
{
int i=0;
int lc1=len(c1);
int lc2=len(c2);
if(lc1<lc2)
{
//are the following two lines allowed?
c1=(char*) malloc(lc1*sizeof(char));
c1=(char*) realloc(c1,lc2*sizeof(char));
for(i=0;i<lc2;i++)
{
*(c1+i)=c2[i];
}
*(c1+lc2)='\0';
}
return c1;
}
*I won't be surprised if this code has multiple issues! * I tried testing it as follows:
#include <stdio.h>
#include <string.h>
#include "mystring.h"
main()
{
int i;
char s1[12],s2[12];
gets(s1);
gets(s2);
printf("copy: %s",copy(s1,s2));
printf("\ns1: %s",s1);
}
I got: output
I wanted both of the outputs to be the same, i.e. copy's and s1's. It happens to be that only this functions only when I print it along with the invokation. I was expecting it to print the copy when I printed s1.
Where have I gone wrong? I think it's something related to the scope of the variables I'm dealing with, or the pointers and memory allocation as a whole!
EDIT: I had made an alternative-function xcopy:
char* xcopy(char c1[])
{
int i=0;
int lc1=len(c1);
char* c2=(char*) malloc((lc1+1)*sizeof(char));
for(i=0;i<lc1;i++)
{
*(c2+i)=c1[i];
}
*(c2+lc1)='\0';
return c2;
}
I realised this function was not solving the actual problem.
Thankyou for your help!!