Thread: Recieving two warnings and one error I cant figure out how to fix

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    29

    Recieving two warnings and one error I cant figure out how to fix

    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.
    Last edited by blackfox_1109; 02-01-2014 at 08:42 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by blackfox_1109
    Code:
    |170|warning: return makes pointer from integer without a cast [enabled by default]
    The above warning warns you that (list->capacity/list->size) is of integer type, but you declared spaceAvaliable as returning a pointer.

    Quote Originally Posted by blackfox_1109
    Code:
    |189|warning: comparison between pointer and integer [enabled by default]
    The above warning warns you that spaceAvaliable returns a pointer, but you are comparing that pointer with an integer when you write (spaceAvaliable(list) < strlen(str)).

    Quote Originally Posted by blackfox_1109
    Code:
    |330|error: expected declaration or statement at end of input
    You need to indent your code properly.

    I repeat my recommendation: first, implement ArrayList to store an array of integers or a single string. Then, you can implement it again to "store an array of strings (i.e., an array of char arrays)". The number of simple mistakes that you have made and cannot easily solve on your own indicates that you have not had the experience with implementing for a 1D dynamic array first. Without that experience, trying to implement for a 2D dynamic array is so much more difficult.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warnings & error messages
    By cfanatic in forum C Programming
    Replies: 39
    Last Post: 08-03-2012, 08:39 PM
  2. Can'nt figure out ERROR
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 01-27-2006, 11:07 AM
  3. I cant figure out how to fix this error
    By Raigne in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 02:04 AM
  4. can't figure this error out
    By the Wookie in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2003, 03:23 PM
  5. Can't figure it out the error??? HELP!!!
    By xeneize in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 03:44 PM

Tags for this Thread