I am writing a program that expands array list whenever they get too full
so far i have this:
Code:
#define DEFAULT 10
typedef struct ArrayList
{
//array of strings
char **array;
//number of elements that have been added to the array
int elements;
// maximum length of array
int maximum;
} ArrayList;
ArrayList *myList(int length)
{
int i;
//dynamically allocates space for a new ArrayList named list
ArrayList *list;
list = (ArrayList*)malloc(sizeof(ArrayList));
//dynamically allocates space for array of size LENGTH or DEFAULT(whichever is greater)
if(DEFAULT < length){
list->array = malloc(sizeof(char *) * length);
}
else{
list->array = malloc(sizeof(char *) * DEFAULT);
//initialize pointers in array to NULL with length
if(DEFAULT < length){
for(i=0; i<length; i++){
list->array[i] == NULL;
}
//set the size and capacity
list->elements = 0;
list->maximum = array[i];
//print out the size of list
printf("Created new ArrayList of size %d.\n", maximum);
//return function
return list;
}
So, ArrayList *myList should return a pointer to the new arraylist or null if malloc fails.
I am confused on what exactly I need to set my maximum to, I know that it shouldn't be 0 and array[i] doesn't seem to be working either. I also am not sure if I am properly setting up null correctly for my array. If there is anything with my code that seems wrong please do point it out as I have been staring at this for hours and am really trying to understand this but I keep hitting a wall.
Thanks!