top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Difference between strdup and strcpy?

+4 votes
539 views

.

posted Nov 9, 2013 by Mona Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

strdup function allocates memory according to the size of input and if memory not available then it will fail,But in case of strcpy while copying the the source string to destination it never checks the size of the destination.So it will be problem if destination size is smaller.

answer Nov 9, 2013 by Sachidananda Sahu
+3 votes

Both copy a string. strcpy wants a buffer to copy into. strdup allocates a buffer using malloc().
Unlike strcpy(), strdup() is not specified by ANSI .

answer Nov 9, 2013 by Vikas Upadhyay
Similar Questions
+3 votes

I have a question about this following code.

char a[4] = {"aaa"};
strcpy(a, "bb");

When I do this after function copies bb to the array it puts '\0' ? I mean last for array would be 'b' 'b' 'a' '\0' or 'b' 'b' '\0' '\0'.

...