### Question 2.6

I came across some code that declared a structure like this:

    struct name {
    	int namelen;
    	char namestr[1];
    };

and then did some tricky allocation to make the `namestr` array act like it had several elements. Is this legal or portable?

This technique is popular, although Dennis Ritchie has called it ``unwarranted chumminess with the C implementation.'' An official interpretation has deemed that it is not strictly conforming with the C Standard. (A thorough treatment of the arguments surrounding the legality of the technique is beyond the scope of this list.) It does seem to be portable to all known implementations. (Compilers which check array bounds carefully might issue warnings.)

Another possibility is to declare the variable-size element very large, rather than very small; in the case of the above example:

    	...
    	char namestr[MAXSIZE];
    	...

where `MAXSIZE` is larger than any name which will be stored. However, it looks like this technique is disallowed by a strict interpretation of the Standard as well.

References: Rationale Sec. 3.5.4.2

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback