[Toybox] Any language lawyers here?

Daniel Walter d.walter at 0x90.at
Wed Feb 22 08:30:13 PST 2012


On Wed, Feb 22, 2012 at 04:36:15PM +0100, Daniel Walter wrote:
> On Wed, Feb 22, 2012 at 07:35:22AM -0600, 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:
> > 
> > temp.c: In function ?main?:
> > temp.c:7: error: invalid application of ?sizeof? to incomplete type ?int[]?
> > 
> > The second of which at least makes sense.  The first is just stupid. I
> > want to have one common instance of the kill signal list, _and_ I want
> > various things to be aware how big the size of the thing is, and the
> > compiler's determined to give me a warning for doing something which I
> > _believe_ the language allows...
> > 
> > Is there some kind of "yes I know what I'm doing, shut up gcc"
> > annotation I can put on that line without having to hardwire in a
> > constant and maintain it by hand?  (I want to make the extern a #define
> > which lib/lib.c can set to semicolon or something, and everybody else
> > can have as "extern"...)
> > 
> > Rob
> hi,
> 
> Why don't you put the array into a header-file ?
> 
> eg:
> temp.c
> 
> #include "temp.h"
> 
> int
> main (void)
> {
>    printf("sizeof(blah)=%d\n", (int)sizeof(blah));
>    return 0;
> }
> 
> temp.h
> static const int blah = {1,2,3,4,5,6}; 
> 

a second option would be
temp.c

extern int blah[];
extern size_t blah_size;

/* lots of code here */

temp1.c
int blah[] = {1,2,3,4,5,6,7};
size_t blah_size = sizeof(blah);

but I still would vote for the header file 

regards,

daniel


 1329928213.0


More information about the Toybox mailing list