I'm writing a program that contains functions to be used by multiple possible main files. Before I test to make sure everything is working correctly I'd like to get rid of anything that could give me any sort of errors. I've searched for how to fix these issues for a while now, but i cant find what exactly is wrong with the code, or how to fix it.
Before I post the functions that have something wrong with them, here are the auxiliary function or structures referenced in the faulty functions:
Code:
typedef struct ArrayList
{
// We will store an array of strings (i.e., an array of char arrays)
char **array;
// Size of list (i.e., number of elements that have been added to the array)
int size;
// Length of the array (i.e., the array's current maximum capacity)
int capacity;
} ArrayList;
Code:
//Expand arraylist to new size
ArrayList *expandArrayList(ArrayList *list, int length){
int i;
char **newarray = NULL;
//find how many elements are currently in the array
int arrsize = sizeof(list->array)/sizeof(list->array[0]);
//dont do anything if the length is smaller than the current capcity
//or if the list was NULL
if (list == NULL)
return NULL;
if (length <= list->capacity)
return NULL;
//create a new (larger) array to store the contents of the old array
newarray = realloc(list->array, sizeof(list->array[0])*length);
//make sure malloc was successful
if (newarray != NULL){
list->array = newarray;
//let user know the function was successful
printf("-> Expanded ArrayList to size %d", length);
//set new size and capacity if they were changed
list->size = arrsize;
list->capacity = length;
//return the pointer to list
return list;}
//if malloc was unsuccessful
else
{
printf("\nexpandArrayList realloc failed\n");
return NULL;
}
}
Code:
//gives the next unoccupied space
int nextAvaliable(ArrayList *list){
if (list->size = list->capacity){
printf("\nError. No more space avaliable\n");
return -1;
}
list->size += 1;
return list->size;
}
NOTE: nextAvaliable and spaceAvaliable are two different function
Here are the functions that are giving errors & warnings, and which warning each function gives:
Code:
//create a function that can tell you the space avaliable in the array
int *spaceAvaliable(ArrayList *list){
return list->capacity/list->size;
}
^ This gives the warning @ the return line:
Code:
|170|warning: return makes pointer from integer without a cast [enabled by default]
Code:
//inserts a copy of string into the next unused cell
char *put(ArrayList *list, char *str){
char *word = NULL;
//if list or string was NULL
if (list == NULL)
return NULL;
if (str == NULL)
return NULL;
//dynamically allocate only the space needed into word
word = malloc(sizeof(char)*(strlen(str)+1));
if (word != NULL){
//if str takes up more space than is avaliable in list->array
if (spaceAvaliable(list) < strlen(str)){
//call expandArrayList function to grow array to needed length
expandArrayList(list, list->capacity*2+1);
//set string capacity to new length
list->capacity = list->capacity *2 + 1;
//insert a copy of str into next unused cell
strcpy(list->array
[list->size+1], str);
//return a pointer to the copy of str
return list->array[nextAvaliable(list)];
}
else {
strcpy(list->array
[list->size+1], str);
return list->array[nextAvaliable(list)];
}
}
else {
printf("\nput malloc failed\n");
return NULL;
}
}
^This gives warning @ if (spaceAvaliable(list) < strlen(str))
Code:
|189|warning: comparison between pointer and integer [enabled by default]
Code:
//print all the strings currently in the array
void printArrayList(ArrayList *list){
int i;
for (i=0; i<list->size; i++){
if (list->array[i] != NULL)
printf("%s\n", list->array[i]);
else
printf("(empty list)\n");
}
}
^This gives error @ the last closing bracket
Code:
|330|error: expected declaration or statement at end of input
Any help would be greatly appreciated.