程序代码:
#define d_array_init_size 10 #define d_array_size_increment 10 typedef int elem_type; typedef struct { elem_type * element; int size; int length; } d_array int init_d_array(d_array a) { a.element = elem_type * malloc(sizeof(int) * d_array_init_size); if (!a.element) { return 1; } a.size = d_array_init_size; a.length = 0; return 0; } int extend_d_array(d_array a) { a.element = elem_type * realloc(a.element, a.length + d_array_size_increment); if (!a.element) { return 1; } a.size += d_array_size_increment; return 0; } int free_d_array(d_array a) { free(a.element); a.element = NULL; a.size = 0; a.length = 0; return 0; }
这样应该就可以了