strcpy vs strdup

strcpy(ptr2, ptr1) is equivalent to while(*ptr2++ = *ptr1++) where as strdup is equivalent to (memcpy version might be more efficient) So if you want the string which you have copied to be used in another function (as it is created in heap section) you can use strdup, else strcpy is enough.

strdup() – what does it do in C?

Exactly what it sounds like, assuming you’re used to the abbreviated way in which C and UNIX assigns words, it duplicates strings ðŸ™‚ Keeping in mind it’s actually not part of the ISO C standard itself(a) (it’s a POSIX thing), it’s effectively doing the same as the following code: In other words: It tries to allocate enough memory … Read more

strdup() – what does it do in C?

Exactly what it sounds like, assuming you’re used to the abbreviated way in which C and UNIX assigns words, it duplicates strings ðŸ™‚ Keeping in mind it’s actually not part of the ISO C standard itself(a) (it’s a POSIX thing), it’s effectively doing the same as the following code: In other words: It tries to allocate enough memory … Read more