Skip to content

Instantly share code, notes, and snippets.

@briankip
Created October 9, 2015 17:14
Show Gist options
  • Save briankip/afc126f8000c94c818b0 to your computer and use it in GitHub Desktop.
Save briankip/afc126f8000c94c818b0 to your computer and use it in GitHub Desktop.
int main()
{
char *str = "blablabla";
char c = 'H';
size_t len = strlen(str);
char *str2 = malloc(len + 1 + 1 ); /* one for extra char, one for trailing zero */
strcpy(str2, str);
str2[len] = c;
str2[len + 1] = '\0';
printf( "%s\n", str2 ); /* prints "blablablaH" */
free( str2 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment