Created
January 20, 2014 12:21
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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