Waht is difference between strdup and strcpy?

Waht is difference between strdup and strcpy?

Some main difference b/w strdup and strcpy are given below:

1.strdup: copy a string to a location that will be created by the function. The function will allocate space, make sure that your string will fit there and copy the string. Will return a pointer to the created area. 
strdup call malloc to allocate storage. Therefor you have to call free when you are not using it.

char name[] = "Welcome";
char* copy = 0;
copy = strdup(name);

2.strcpy:copy a string to a location YOU created (you create the location, make sure that the source string will have enough room there and afterwards use strcpy to copy)
strcpy copy from source to destination without doing overflow checking.

char name[255];
strcpy(name, "Welcome");

By:Vivek Kr. Agarwal
Date:2002-01-09 00:00:00

Post Your Answers

User Email:

User Name:

Related C Subjective Links

C Subjective interview questions and answers for experienced and fresher