Code:
static char *Token[MAXSYMBOLS];
...
char g_symbols[MAXSYMBOLS];
It looks like the function is returning a set of string starts that should be assigned differently to the g_symbols variable. In C a string, a true string is character array allocated either statically or dynamically to which we know the start and that at some point has a special character '\0' to mark it's end. When a function returns an array of string starts, based on a given true string, like in this example, it returns merely something equivalent to page-markers in a book. But someone who needs to study the things the page-markers show would go to get a printed copy of all the "marked" pages. Every Token[i] is the same as a page-marker.
g_symbol, is just a set of page-markers.
g_symbols, is an array of characters, or in other words a fixed width page.
To store the return value of foo() it would be necessary to get some blank pages and get a copy of the contents that the page-markers foo() returns indicate.
char g_symbols[MAXSYMBOLS], is not a good type to do that.
The best would be either:
Code:
char g_symbols[MAXSYMBOLS][MAXSYMBOLSIZE];
//or
char *g_symbols[MAX_SYMBOLS];
On the first one could get hard copies by saying using strcpy() with target every g_symbols[i] and source every g_symbol[i]. The problem is that if a g_symbol[i] has more length than our MAXSYMBOLSIZE constant, we would get overflow and corrupted data.
On the second, one could either malloc() every g_symbols[i] to the appropriate string length()+1 for the ending '\0'. or use a more elegant in my opinion call to strdup(), like:
Code:
// Case 1
...
strcpy(g_symbols[i], g_symbol[i]); /* Buffer overflow danger */
...
// Case 2 solution 1
g_symbols[i] = malloc(strlen(g_symbol[i]) + 1);
strcpy(g_symbols[i], g_symbol[i]); /* Ok, memory is enough and never fails */
...
// Case 2 solution 2, notice how much it looks like case 1.
g_symbols[i] = strdup(g_symbol[i]); /* Guaranteed to work if we have memory */
So, it comes down that duplicating an array of string is as simple as to repeat a simple operation. The only things that needs to be correct to use this elegant and safe 99.99% of the time solution, is the type of an array of string starts, which is: char *str_array[N].
The memory allocation management is "handled" by strdup, and the programmer need release the pointers himself afterwards. That simple.