Thread: Issues with character arraylists

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    29

    Issues with character arraylists

    I'm working on a program right now that requires me to do a couple of things. First, I have a structure with a couple things in it such as a character arraylist, and then information on that list such as size and capacity.
    For one of the functions I have to write that uses that structure, I have to create an instance of that structure, and return a pointer to it.

    If I created a function that that created an instance of the structure and I labeled it *L, and then filled it with the inputed information, to return the pointer to the new structure, would it be as simple as:
    Code:
    return *L;
    And I have another function I have to make that requires me to expand the arraylist, and copy the contents of one character arraylist into another. In order to do so, I thought I would have to do
    Code:
    for (i=0; i<length; i++){
    strcpy(newarray[i], array[i]);
    }
    Would this work? Would I need to treat it similar to that of an integer array and do
    Code:
    for (i=0; i<length; i++){
    newarray[i] = array[i];
    }
    Im sorry I cant just post my code, this is for a programming assignment, and I don't want to do anything that might constitute cheating.

    Edit: I've gone back and added my full code so far.

    Here is the initial structure:
    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;
    And "Arraylist.h" is what has the structure definition I included above as well as the function prototypes.

    Code:
    //included libraries
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "ArrayList.h"
    //Create arraylist
    ArrayList *createArrayList(int length){
    //Dynamically allocate space for new ArrayList
        ArrayList *L = malloc(sizeof(ArrayList));
        int i;
        //initialize to NULL
        L->array = NULL;
        if (length >= DEFAULT_INIT_LEN)
        {
            L->array = malloc(sizeof(char*)*length);
            if (L->array != NULL)
                {
                    printf("-> Created new ArrayList of size %d\n",length);
        //initialize pointers in array to NULL
                    for (i=0; i<length; i++){
                        L->array[i] = NULL;
                    }
        //Set size and capacity
                    L->size = 0;
                    L->capacity = length;
                    return *L;
                }
        //In case malloc fails
            else{
                    printf("\n malloc failed.\n");
                return;}
        }
        //if length is smaller than default size
        else
        {
            L->array = malloc(sizeof(char*)*DEFAULT_INIT_LEN);
            if (L->array != NULL)
                {
                    printf("-> Created new ArrayList of size %d\n",DEFAULT_INIT_LEN);
                    L->size = 0;
                    L->capacity = DEFAULT_INIT_LEN;
                    for (i=0; i<DEFAULT_INIT_LEN; i++){
                        L->array[i] = NULL;
                    }
                return *L;
                }
            else{
            printf("\nmalloc failed.\n");
                return;}
        }
    }
    //Destroy arraylist
    ArrayList *destroyArrayList(ArrayList *list){
    int i;
    //free everything in the array first, then free the array itself
    for (i=0; i<list->capacity; i++){
        free(list->array[i]);
    }
    free(list->array);
    free(list->size);
    free(list->capacity);
    free(list);
    //set list to NULL and return list
    list = NULL;
    return *list;
    }
    //Expand arraylist to new size
    ArrayList *expandArrayList(Arraylist *list, int length){
    int i;
    char newarray = malloc(sizeof(int*)*length);
    if (length <= list->capacity)
        return;
    if (*list == NULL)
        return;
    for (i=0; i<length; i++){
    strcpy(newarray[i], list->array[i]);}
    
    //free the orginal array, since it has been copied over to the new array
    for(i=0; i<list->capacity; i++){
            free(list->array[i]);
            }
    free(list->array);
    //set list->array to point to the newly created array
    list->array = &newarray;
    printf("-> Expanded ArrayList to size %d", length);
    list->size = _;
    list->capacity = ___;
    return list->**array;
    }
    //trim the arraylist if it's capacity is larger than its current size
    ArrayList *trimArrayList(ArrayList *list){
    if (list->capacity <= list->size )
        return;
    char newarray = malloc(sizeof(char*)*list->size);
    for (i=0; i<list->size; i++){
        newarray[i] = list->array[i];
    }
    printf("-> Trimmed ArrayList to size %d", list->size);
    }
    And an 'arraylist' is the term my professor used for an array of arrays.
    Last edited by blackfox_1109; 01-31-2014 at 01:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-05-2011, 08:21 PM
  2. Character counting issues from Kernighan text
    By Vampireclown in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 10:29 AM
  3. multi-character character constant error
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 07-15-2007, 12:12 AM
  4. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  5. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM

Tags for this Thread