Daniel Bush was once rumoured to have said:
> Wow, I remember when this thread had just 3 messages.
> Anyway, just trying to clarify from the above code: "string2" is a pointer
> to an automatic variable - a character array? Very bad to pass this address
> back to main().
> But if you had said
> char * string2 = "some words"
> or even
> auto char * string2 = "some words"
> it will work (at least as I've just tested with gcc) because you've
> initialised a pointer to a string literal, which pretty much is set in stone
> for the life of the program. (Nor can you alter its contents)

Correct, but at the same time, the behaviour is not reliable.

IIRC, ANSI C[1] makes no guaranty as to the lifetime of literal
strings when their enclosing scope finishes.  And not all literal
strings are 'static' as my code demonstrated.

C.
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to