Strdup
The strdup() function duplicates a string
Strlen
A standard C function that returns the length of a string.
Others
Example |
---|
Additionally if you have strdup then it is much more convenient than strlen + malloc + strcpy with identical result including the same obligation to free the allocated storage when you no longer need it from question How do I store my substring between strtok tokens in C? |
In this case strdup is better;and you don strlen actually copy the string returned by strtok you copy the pointer from question Split and Join strings in C Language |
As i noted in comments however if you re willing to rely on posix s strdup then that s cleaner than strlen + malloc + and has the same semantics you take responsibility for freeing the memory allocated for the copy from question Conditional jump or move depends on uninitialised value(s) in C |
C strings are supposed to be nul-terminated and strlen doesn t account for this;perhaps using strdup would be better here from question Linked list: strange muddle of member element during output |