In my main:
char *s1 = "Brockport";
char *t1 = "SUNY";
printf("\n---In string Copy---\n");
printf("Before string copy s = %s and t = %s\n",s1,t1);
if(my_strcpy(s1,t1) == 1){
printf("String copied successfully\n");
printf("After string copy s = %s and t = %s\n",s1,t1);
}
else if(my_strcpy(s1,t1) == -1){
printf("String not copied\n");
printf("After string copy s = %s and t = %s\n",s1,t1);
}
else
printf("\n Unknown error!\n");
So the main calls this function with those parameters and it goes here (where the problem occurs):
int my_strcpy(char *s, char *t)
{
//find the length of both strings
int first_string = strlen(s);
int second_string = strlen(t);
//in this case the string will not be copied
if(first_string < second_string)
{
return -1;
}
//Assign each positino in t the corresponding position in s
for(int x = 0; x <= second_string; x++)
{
*(s + x) = *(t + x);
}
//it works so return 1
return 1;
}
When I run this code, this is the output:
[email protected]% ./zhine (~/workspace/csc311) 11:35 am ---In string Copy--- Before string copy s = Brockport and t = SUNY zsh: segmentation fault (core dumped) ./zhine

New Topic/Question
Reply
MultiQuote







|