960 views
0 votes
0 votes

I am trying to implement strcpy manually but am getting error. What is the error? Please help!
 

#include<stdio.h>
char* cpy(char *a,char *b)
{
    char *arr;
    arr=&a;
    while((*a++=*b++)!='\0');
     printf("%s", arr);

}
int main()
{
   char s[15]="Sreeja";
   char t[15]="Mukherjee";
  cpy(&s,&t);
 
}

 
 

1 Answer

Best answer
4 votes
4 votes
#include<stdio.h>
char* cpy(char *a,char *b)
{
    char *arr;
    arr=&a;                               // Error
    while((*a++=*b++)!='\0');
    printf("%s", arr);                 
}

int main()
{
   char s[15]="Sreeja";
   char t[15]="Mukherjee";
   cpy(&s,&t);
}
 

Error:

A single pointer cannot hold an address of a pointer, you would need a double pointer for that. However, here, all you want is to store the address to which pointer 'a' is pointing to. This is needed because it is going to be incremented and you need that for printing (and of course returning).

To store that address which is pointed by 'a', into another pointer 'arr', this is sufficient

arr=a;
selected by

Related questions

1 votes
1 votes
2 answers
3
0 votes
0 votes
0 answers
4
shiva0 asked Jan 19, 2019
449 views
int main(){ int a=2,b=2,c=2; printf("%d",a==b==c); return 0;}what is the output??? and what will be done during executioncan anyone explain this……...