[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Tinycc issues

From: Henrik Nordstrom
Subject: Re: [Tinycc-devel] Tinycc issues
Date: Wed, 16 Mar 2005 11:36:35 +0100 (CET)

On Tue, 15 Mar 2005, Dominic Mazzoni 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
    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.

For this you should forward declare the existence of the array, not the array as such...

extern/static int g_primes[];


int g_primes[] = {2, 3, 5, 7, 11, 13, 17, 23};

This assumes the array is at global or file scope, which is about the only situation where this kind of declarations makes sense. In local scope you better rearrange the code to have the definition in the proper place avoiding the need of forward declaration.

In your specific example with function pointers you can also solve the problem by forward declaring the functions you want to include in the array rather than the array. IMHO In many cases this also makes thre code more readable.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]