Hi C Cders,
I have a query in C Pointers.
I have written a simple strcpy program.
So from src to dest, it copies.
Now if I do not assign the src string say it is likeCode:#include <stdio.h> #include <conio.h> void my_strcpy(char *dst, char *src) { if(src == NULL) { printf("Null Assignment:\n"); return; } while(*src != '\0') { *dst = *src; src++; dst++; } *dst = '\0'; } int main() { char dst[20]; char *src = "Hello"; my_strcpy(dst, src); printf("%s\n", dst); getch(); }
Then in the printf statement it will print some junk chars.Code:char *src;
So how to handle this type of cases.
As this case src may hold any address, it may be either NULL or some arbitrary address. So with NULL checking, it will not suffice.
So in the my_strcpy() what check need to add?
Thanks in advance for your help.
With regards,
u_peerless