Thread: How to add a char array to an existing array of char arrays?

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    9

    How to add a char array to an existing array of char arrays?

    Sorry the title sounds like a tongue twister. If I have an array of arrays like so:

    Code:
        char *specials[24] = {
            "and", "or", "not", "equal", "plus", "minus",
            "times", "slash", "dollar", "percent", "at",
            "zero", "one", "two", "three", "four", "five",
            "six", "seven", "eight", "nine", "to", "for", "ate"
        };
    Whats the proper way to append another word/char array to specials? Hopefully this hasn't been asked a lot. A search of "append char array to array of char arrays" brings up a lot of stuff that isn't necessarily related to my question.

    Edit:

    I added a function like this:

    Code:
    void append_word(char **wordarray, int length) {
        wordarray[0] = "new_word";
    }
    This works and inserts new text. I tried using the length to try wordarray[length+1] = "new_word"; but that gives me an error unfortunately.

    Any help would be greatly appreciated. Thank you.
    Last edited by StruggleBot; 03-16-2019 at 03:52 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The normal way would be to create "specials" using malloc()

    You would then resize your original memory allocation using realloc()
    Fact - Beethoven wrote his first symphony in C

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since specials is an array of string literals, you should have declared it as:
    Code:
    const char *specials[] = {
        "and", "or", "not", "equal", "plus", "minus",
        "times", "slash", "dollar", "percent", "at",
        "zero", "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "to", "for", "ate"
    };
    Of course, if you do this the array is of fixed size, and moreover the number of elements in use is equal to the size, so you cannot cannot add any more elements in use. If you can anticipate a reasonable maximum number of elements though, you could then write:
    Code:
    const char *specials[100] = {
        "and", "or", "not", "equal", "plus", "minus",
        "times", "slash", "dollar", "percent", "at",
        "zero", "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "to", "for", "ate"
    };
    
    int append_word(const char **words, size_t size, const char *word) {
        size_t i = 0;
        while (i < size && words[i])
        {
            ++i;
        }
    
        if (i < size)
        {
            words[i] = word;
            return 1;
        }
        else
        {
            return 0;
        }
    }
    Now you can write:
    Code:
    if (append_word(specials, 100, "new_word"))
    {
        // etc
    }
    else
    {
        // report/log error concerning the specials array being full
    }
    Alternatively, you could use Click_here's suggestion of malloc (and realloc), in which case specials would be a const char** instead of an array. Remember to free what you malloc.
    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

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    9
    Ah that's pretty neat... thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 03-20-2016, 05:17 AM
  2. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  3. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  4. How to get length of array of char arrays
    By nicoeschpiko in forum C Programming
    Replies: 3
    Last Post: 03-13-2010, 12:53 PM
  5. Replies: 3
    Last Post: 11-17-2008, 12:36 PM

Tags for this Thread