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.