Skip to content

Instantly share code, notes, and snippets.

@ffoxin
Created January 20, 2014 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffoxin/8519064 to your computer and use it in GitHub Desktop.
Save ffoxin/8519064 to your computer and use it in GitHub Desktop.
str_cpy function for "5 minute challege" by hola.org Requirements: http://thecodeil.com/
char* str_cpy(char** dest, const char* source)
{
unsigned int dest_size = *dest ? strlen(*dest) : 0;
unsigned int source_size = strlen(source) + 1;
/* check if source is a part of dest - no need to reallocate memory */
if (((unsigned int)(source - *dest)) < dest_size)
return memmove(*dest, source, source_size);
/* reallocate since source is possibly larger than dest */
if (source_size > dest_size)
*dest = (char *)realloc(*dest, source_size);
return memcpy(*dest, source, source_size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment