Forked from pastagatsan/gist:1d56eb280e3aa05b8572
Last active
August 27, 2017 10:58
-
-
Save Sebbyastian/6479a5985181ea74dffe to your computer and use it in GitHub Desktop.
Nice, very nice... Looks like fun to me :)
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
typedef struct | |
{ | |
size_t size; | |
void** list; | |
} List; | |
int add(List** li, void* obj) | |
{ | |
List* list = *li ? *li : malloc(sizeof *list); | |
if (list == NULL) { | |
return 0; | |
} | |
*li = list; | |
size_t old_size = *li ? *li->size : 0, new_size = old_size + 1; | |
if ((old_size & new_size) == 0) | |
{ | |
void **new_list = realloc(list->list, 2 * new_size * sizeof *list->list); | |
if (new_list == NULL) { | |
return 0; | |
} | |
list->list = new_list; | |
} | |
list->list[old_size] = obj; | |
list->size = new_size; | |
} | |
void destroy_list(List** li) | |
{ | |
free(*li->list); | |
free(*li); | |
*li = NULL; | |
} | |
int main(void) | |
{ | |
List *l = NULL; | |
add(&l, (int[]) { 25 }); | |
add(&l, (int[]) { 56 }); | |
printf("%d", ((int *) l->list[1])[0]); | |
destroy(&l); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment