Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] C Gurus


Ian Wienand wrote:

On Tue, Nov 22, 2005 at 10:12:07PM +1100, Crossfire wrote:
IIRC, ANSI C[1] makes no guaranty as to the lifetime of literal
strings when their enclosing scope finishes.

I'm fairly sure ANSI C does, C99 definitely does

And not all literal strings are 'static' as my code demonstrated.

String literals are defined with static storage duration by
definition.

C99 6.4.5.5

The multibyte character sequence [string literal] is then used to
initalize an array of static storage duration and length just
sufficient to contain the sequence.

Where static storage duration is defined in 6.2.4.3

Its lifetime is the entire execution of the program and its stored
value is initalized only once, prior to program startup.

So it seems quite valid (as you probably know anyway it will be put in
some read only section which isn't going to go away).  But the code in
question will have an interesting alternative property that it will
confuse every single programmer who looks at the code for the rest of
eternity.

Not confusing if that programmer understands pointers and pointer arithmetic as
well as know and understand that:
1. "variable storage class of static" has a lifetime of the program regardless of which function it is located. By the way this convention is there in K&R which later became ANSI C. In fact, this is one of the very first rules we learn in Variable
Storage Class Static.

2. An initialised "variable storage class" is static by definition.

3. A function can be typed like any variable storage class.

4. The functionality of  "return".

O Plameras