|
From: | Dominic Mazzoni |
Subject: | Re: [Tinycc-devel] Tinycc issues |
Date: | Tue, 15 Mar 2005 16:05:20 -0800 |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040616 |
Neil Bradley wrote:
2. Forward-declare arrays: With gcc, I am allowed to forward-declare an array of unspecified size, and then actually specify the size (implicitly) later in the code. For example: int g_primes[]; ... int g_primes[] = {2, 3, 5, 7, 11, 13, 17, 23};Why would one do this?
Simple example: I want to define the array in a header file, but initialize it in a C file. I don't want to prespecify the number of elements in the array because it's something that changes often, and I want to be able to change it without recompiling any other C files.
Actual example: here's something closer to the code I was actually dealing with - simplified for this example, of course, but note that g_functions is referenced by one of the functions that appears in its own declaration. Ideally I'd like to be able to add a new function to my list without having to remember to add one to a constant NUM_FUNCTIONS or something like that.
- Dominic typedef struct { char *name; eval_fn_t eval_f; } function_t; function_t g_functions[]; float sin_eval(float x, char *unused) { return sin(x); } float cos_eval(float x, char *unused) { return cos(x); } float negate_eval(float x, char *fn) { int i; while(g_functions[i].name) if (!strcmp(g_functions[i].name, fn)) return -(*g_functions[i].eval_f)(x); return NAN; } function_t g_functions[] = { {"sin", sin_eval}, {"cos", cos_eval}, {"negate", negate_eval}, {NULL, NULL} };
-->Neil------------------------------------------------------------------------------- Neil Bradley "I've got a fever and the only prescription is MORESynthcom Systems, Inc. COWBELL!!!" _______________________________________________ Tinycc-devel mailing list address@hidden http://lists.nongnu.org/mailman/listinfo/tinycc-devel
[Prev in Thread] | Current Thread | [Next in Thread] |