banner



How To Add Char Arrays In C

  1. #i

    StruggleBot is offline

    Registered User


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

    Lamentable the title sounds like a natural language twister. If I have an array of arrays similar so:

    Code:

                              char *specials[24] = {         "and", "or", "not", "equal", "plus", "minus",         "times", "slash", "dollar", "percent", "at",         "nothing", "one", "ii", "three", "four", "five",         "six", "seven", "eight", "nine", "to", "for", "ate"     };
    Whats the proper way to suspend another discussion/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 similar 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 effort wordarray[length+1] = "new_word"; but that gives me an error unfortunately.

    Whatever aid would exist profoundly appreciated. Thank y'all.

    Last edited by StruggleBot; 03-16-2019 at 03:52 PM.


  2. #2

    Click_here is offline

    TEIAM - problem solved


    The normal way would exist to create "specials" using malloc()

    You would so resize your original retentivity allocation using realloc()

    Fact - Beethoven wrote his first symphony in C


  3. #3

    laserlight is offline

    C++ Witch laserlight's Avatar


    Since specials is an array of string literals, you should have declared information technology 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", "ix", "to", "for", "ate" };
    Of course, if you do this the assortment is of fixed size, and moreover the number of elements in use is equal to the size, then you lot cannot cannot add any more elements in employ. If you can conceptualize 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", "per centum", "at",     "nix", "one", "two", "three", "four", "5",     "six", "seven", "8", "ix", "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 ane;     }     else     {         return 0;     } }
    Now you can write:

    Code:

    if (append_word(specials, 100, "new_word")) {     // etc } else {     // report/log fault concerning the specials assortment being full }
    Alternatively, y'all could use Click_here's suggestion of malloc (and realloc), in which case specials would be a const char** instead of an array. Call back to gratuitous what you malloc.

    Quote Originally Posted past Bjarne Stroustrup (2000-10-fourteen)

    I get maybe two dozen requests for assistance with some sort of programming or design problem every day. Most take more sense than to transport 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 programme that demonstrates the error" is a powerful debugging tool.

    Look up a C++ Reference and learn How To Inquire Questions The Smart Way


  4. #iv

    StruggleBot is offline

    Registered User


    Ah that's pretty neat... cheers


How To Add Char Arrays In C,

Source: https://cboard.cprogramming.com/c-programming/177229-how-add-char-array-existing-array-char-arrays.html

Posted by: halcombpuffined.blogspot.com

0 Response to "How To Add Char Arrays In C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel