Let's say I wanted to duplicate a string and then concatenate its value.
Using stl std :: string, this is:
string s = "hello" ; string s2 = s + " there" ;
in C:
char* s = "hello" ; char* s2 = strdup( s ) ; strcat( s2, " there" ) ;
The only way I know to do this in C is:
char* s = "hello" ; char* s2=(char*)malloc( strlen(s) + strlen( " there" ) + 1 ) ;
Is there a more elegant way to do this in C?
source share