Below is our list example code.
list.h
#ifndef __LIST_H__
#define __LIST_H__
typedef struct {
unsigned length;
int *array;
} List;
List *makeList();
void destroyList(List *);
void append(List *list, int val);
#endif
list.c
#include <stdlib.h>
#include "list.h"
List *makeList() {
List *ret = (List *) malloc(sizeof(List));
ret->length = 0;
ret->array = (int *) calloc(ret->length, sizeof(int));
return ret;
}
void destroyList(List *list) {
free(list->array);
free(list);
}
void append(List *list, int val) {
list->length += 1;
list->array = (int *) realloc(list->array, list->length * sizeof(int));
list->array[list->length - 1] = val;
}
useList.c
#include <stdio.h>
#include "list.h"
int main() {
List *myList = makeList();
append(myList, 42);
append(myList, 175);
printf("%p\n", myList);
for (int i = 0; i < myList->length; i+=1) {
printf("%d\n", myList->array[i]);
}
puts(""); // To get a new line
return 0;
}