How To Add Char Arrays In C
-
03-16-2019 #i
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:
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.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" };Edit:
I added a function similar this:
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.Code:
void append_word(char **wordarray, int length) { wordarray[0] = "new_word"; }Whatever aid would exist profoundly appreciated. Thank y'all.
Last edited by StruggleBot; 03-16-2019 at 03:52 PM.
-
03-xvi-2019 #2
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
-
03-17-2019 #3
C++ Witch
Since specials is an array of string literals, you should have declared information technology as:
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[] = { "and", "or", "not", "equal", "plus", "minus", "times", "slash", "dollar", "percent", "at", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "ix", "to", "for", "ate" };
Now you can 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; } }
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.Code:
if (append_word(specials, 100, "new_word")) { // etc } else { // report/log fault concerning the specials assortment being full }
Originally Posted past Bjarne Stroustrup (2000-10-fourteen) Look up a C++ Reference and learn How To Inquire Questions The Smart Way
-
03-17-2019 #iv
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