Skip to content

Instantly share code, notes, and snippets.

@khajavi
Last active December 15, 2015 19:19
Show Gist options
  • Save khajavi/5310159 to your computer and use it in GitHub Desktop.
Save khajavi/5310159 to your computer and use it in GitHub Desktop.
c-string append (concat)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char* append( const char* str1, const char* str2 ) {
const size_t str1_length = strlen( str1 );
const size_t str2_length = strlen( str2 );
const size_t total_length = str1_length + str2_length;
char *const str_buff =(char*) malloc( total_length + 1 );
if ( str_buff == NULL ) {
fprintf( stderr, "malloc failed\n" );
exit( EXIT_FAILURE );
}
strcpy( str_buff, str1 );
strcpy( str_buff + str1_length, str2 );
return str_buff;
}
int main( void ) {
puts( append( "Milad", " Khajavi" ) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment