diff --git a/includes/list.h b/includes/list.h index dcff1f7..6d01119 100644 --- a/includes/list.h +++ b/includes/list.h @@ -14,8 +14,8 @@ typedef struct { } List; List* CreateList(void); -int AddListItem(char *, List *); -void PrintList(List*); +int AddListItem(const char *, List *); +void PrintList(const List*); void DestroyList(List*); #endif \ No newline at end of file diff --git a/src/list.c b/src/list.c index fa5fbbf..1fb806b 100644 --- a/src/list.c +++ b/src/list.c @@ -9,11 +9,17 @@ List* CreateList() { return new; } -int AddListItem(char *value, List* list) { +int AddListItem(const char *value, List* list) { if (list == NULL) return -1; if (list->capacity < list->size + 1) { - list->root = realloc(list->root, sizeof(char*) * list->capacity * 2); + void* ptr = realloc(list->root, sizeof(char*) * list->capacity * 2); + //Note: realloc will free list->root if it succeeds. + if (!ptr) { + return -1; + } + + list->root = ptr; list->capacity = list->capacity * 2; } @@ -28,10 +34,10 @@ int AddListItem(char *value, List* list) { return 0; } -void PrintList(List* list) { +void PrintList(const List* list) { if (list == NULL) return; - List *current = list; + const List *current = list; printf("Size: %d; Capacity: %d\n", list->size, list->capacity);