Help with struct code!

This is a discussion on Help with struct code! within the C Programming forums, part of the General Programming Boards category; I am writing a program that expands array list whenever they get too full so far i have this: Code: ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    17

    Lightbulb Help with struct code!

    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!

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    For me it would be logical to set maximum to the number of char * you have actually allocated.
    e.g. whatever is greater DEFAULT or length.
    So I would set list->maximum before initializing the pointers to NULL and then initialize from 0 to < list->maximum
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. where do i define a struct in my code using an array class
    By c++noob145 in forum C++ Programming
    Replies: 4
    Last Post: 03-17-2013, 12:42 AM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. very very simple code (struct question)
    By tt29 in forum C Programming
    Replies: 1
    Last Post: 04-03-2011, 10:33 PM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Game Code Messed Up (Mainly struct{};)
    By CodeNinja in forum C++ Programming
    Replies: 11
    Last Post: 07-19-2004, 08:08 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21