SLUG Mailing List Archives
Re: [SLUG] C Gurus
- To: slug@xxxxxxxxxxx
- Subject: Re: [SLUG] C Gurus
- From: jam@xxxxxxxxx
- Date: Tue, 22 Nov 2005 17:18:57 +0800
- User-agent: KMail/1.8
This is fun and educational, so no-swords-at-dawn but
Doing stuff 'cause it works <ouch>!
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.
And yes too on style (I agree with your sentiment).
Anyway thanks for discussion <smile>
On Tuesday 22 November 2005 14:49, Benno wrote:
> On Tue Nov 22, 2005 at 17:35:23 +1100, Benno wrote:
> >On Tue Nov 22, 2005 at 13:51:59 +0800, jam@xxxxxxxxx wrote:
> >>Ummmm ...
> >>Coding 3.
> >>#include <stdio.h>
> >>#include <stdlib.h>
> >>#include <string.h>
> >>char *
> >> char *string2 = "some words";
> >> return string2;
> >>int main (void)
> >> char *string;
> >> string = somefunction();
> >> printf ("\n\nString is: %s\n\n", string);
> >> return 0;
> >>somefunction returns string2 which is trash!
> >>formally: The scope of string2 does not extend to main!
> >>(even if it works, and it might, it's WRONG)
> >"some words" will be allocated in the .rodata section not on the stack
> >so it will actually work. (Not that I'd recommend doing this!!).
> >I can't find anything in the C spec about return the address of a string
> > literal, it doesn't say wether it is allowed or not.
> I can't actually find it in the C spec but from googling it does appear
> that string literals are constants with static storage duration which
> means they have lifetime of the program.
> (But I still wouldn't use that style, it is confusing at best.)