Interview Question
AnalystsCountry: India
Interview Type: In-Person
This will works.
Allocate the memory in main()
#include<stdio.h>
#include<stdlib.h>
void mystrcpy( char *string2 ,char *string1 )
{
while( *string1 != '\0')
{
*string2++= *string1++;
}
}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
str2=malloc(20); //Allocate memory here
mystrcpy( str2,str1);
printf("string1 = %s string2 =%s ",str1,str2) ;
return 0;
}
So here are two different working implementations of your code
#include<stdio.h>
#include<stdlib.h>
void mystrcpy( char **string2 ,char *string1 )
{
*string2 = malloc(20);
char *str = *string2;
while( *string1 != '\0')
{
*str++ = *string1++;
}
}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
mystrcpy(&str2,str1);
printf("string1 = %s string2 =%s ",str1,str2);
return 0;
}
AND
#include<stdio.h>
#include<stdlib.h>
char* mystrcpy( char *string2 ,char *string1 )
{
string2 = malloc(20);
char *string = string2;
while( *string1 != '\0')
{
*string2++= *string1++;
}
return string;
}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
str2 = mystrcpy( str2,str1);
printf("string1 = %s string2 =%s ",str1,str2);
return 0;
}
malloc() after successful allocation returns (void*).since we are trying to allocate memory to character strings which needs memory to be of (char*). so,typecasting is required to malloc() function by (char*). otherwise compiler can not interpret which type of variable we are allocating hence returns null.
malloc() after successful allocation returns (void*).since we are trying to allocate memory to character strings which needs memory to be of (char*). so,typecasting is required to malloc() function by (char*). otherwise compiler can not interpret which type of variable we are allocating hence returns null.
Correct code
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
char * mystrcpy( char *string2 ,char *string1 )
{
string2 = (char*)malloc(20);
char * temp = string2;
while( *string1 != '\0')
{
*temp= *string1;
temp++;
string1++;
}
return string2;
}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
char *str3 = mystrcpy( str2,str1);
printf("string1 = %s string2 =%s, str3 = %s ",str1,str2,str3) ;
getchar();
return 0;
}
correct code :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* mystrcpy( char *string2 ,char *string1 )
{
int l,i;
string2 = (char*)malloc(20);
l = strlen(string1);
i=0;
while(i<l)
{
*(string2+i)=*(string1+i);
i++ ;
}
*(string2+i)='\0';
//string2 = "hiii";
return(string2);
}
int main()
{
printf("****************program starts*************************\n");
char *str1 ="hello world";
char *str2 =NULL;
str2 = mystrcpy( str2,str1);
printf("string1 = %s\nstring2 =%s ",str1,str2) ;
return 0;
}
C uses pass by value.
- Subbu. November 19, 2013So mystrcpy gets a copy of str2 (string2), which you overwrite with the result of malloc. This does nothing to str2 of main, which continues to keep its old value, which is NULL.