[Toybox] Any language lawyers here?

Andre Renaud andre at bluewatersys.com
Wed Feb 22 10:52:53 PST 2012


Hi Rob,

On 23/02/12 02:35, Rob Landley wrote:
> I'm arguing with C:
> 
> If I do the equivalent of:
> 
> extern int blah[] = {0,1,2,3,4};
> 
> int main(int argc, char *argv[])
> {
>   printf("sizeof(blah)=%d\n", (int)sizeof(blah));
>   return 0;
> }
> 
> I get:
> 
> temp.c:3: warning: ‘blah’ initialized and declared ‘extern’
> 
> But if I just have extern into blah[] without the initialization, I get:

Slightly more awkward, but the other option is to declare the array size
with the extern, but put the initialisation in the .c file. This means
that everyone knows how big it is, but there is only one instance (so
less data space taken).
The disadvantage is you're splitting the information in two places - the
declaration & the initialisation.

ie:
foo.h:
extern int buf[12];

foo.c:
#include "foo.h"
int buf[] = {1, 2, 3, 4, 5, 6, 7};

int main(void)
{
    printf("sizeof: %d\n", sizeof(buf));
    return 0;
}

If you end up shrinking the initialisation, you have to remember to fix
it in the header file as well. If you end up increasing the
initialisation, then GCC will warn you that you've gone over the
declared bounds.

Andre

 1329936773.0


More information about the Toybox mailing list