Skip to content

Instantly share code, notes, and snippets.

@milesrout
Last active May 29, 2016 05:20
Show Gist options
  • Save milesrout/561ea1da7aa9d214842b to your computer and use it in GitHub Desktop.
Save milesrout/561ea1da7aa9d214842b to your computer and use it in GitHub Desktop.
int wcsArrayAdd(wchar_t ***array, int *cur_size, wchar_t *item)
{
int new_size = (*cur_size) + 1;
size_t len = wcslen(item);
wchar_t *tmp1 = NULL;
wchar_t **tmp2 = NULL;
/* alloc memory for item */
tmp1 = malloc((len + 1) * sizeof(wchar_t));
if (tmp1 == NULL) {
return ERROR_MEMORY;
}
tmp1 = wcscpy(tmp1, item);
/* (re)alloc memory for ptr array */
tmp2 = realloc(*array, new_size * sizeof(wchar_t*));
if (tmp2 == NULL) {
free(tmp1);
return ERROR_MEMORY;
}
*array = tmp2;
*array[new_size - 1] = tmp1;
*cur_size = new_size;
tmp1 = NULL;
tmp2 = NULL;
return ERROR_OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment