On Tue Nov 22, 2005 at 17:18:57 +0800, [EMAIL PROTECTED] wrote:
>Benno
>This is fun and educational, so no-swords-at-dawn but
>Doing stuff 'cause it works <ouch>!
Yeah, definately a bad idea!
>Look at C implemented on some utterly horrid hardware (pics and tiny-atmels)
>and the known way does not work. The only way to write robust C is to
>pedantically follow the rules. I hoped to show a newby that.
Couldn't agree more. Its just that in this case the "rule" isn't
100% clear, and reading my handy C99 spec doesn't actually
illuminate the subject.
The tricky thing is that it doesn't actually say in the spec
(well anywhere I could find), what the storage type is
for string literals. And I think technically that a string literal
there doesn't allocate memory in function scope but in global scope.
I *think* according to the rules it is equilant to:
const static char __magic_string[] = "some words";
char * fn(void) { char *s = __magic_string; return s; }
Which I think you would agree is correct, (but again doesn't exactly
buy you much).
But then again it could be such that a C compiler is allowed to treat it
as:
char * fn(void) {
char __magic_string[] = "some words";
char *s = __magic_string;
return s;
}
Where it is clearly wrong.
In my experience compilers tend to do it the first way, but that doesn't mean
they couldn't do it the second way.
I'm just really annoyed that I can't find the clause in the spec that
gives the answer one way or the other.
I'm glad you brought it up, I've learned more about thigns about C that I didn't
even know I didn't know ;).
Benno
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html