O Plameras was once rumoured to have said:
> Programming is about coding to produce the stated results.
>
> Your code is silly as far as the objective of the program is concerned.
What? the objective I set for my program was quite straight forward.
Obviously you failed to observe what it was, and mistook it for your
objective.
The point of the demonstration is why you never return pointers of
locally scoped data from a function. It may look safe superficially,
but it isn't.
> When you try to compile your code there are warnings which means your
> code is not ready to be ran.
No. Warnings can be misleading. Especially if warning against the
desired effect. They are warnings afterall. Warnings are also not
the final level of protection against stupidity. Take this code for
example:
---BEGIN---
#include <stdio.h>
unsigned long *
foo()
{
static unsigned long magic = 0x1234;
return &magic;
};
int
main()
{
unsigned short * dptr;
dptr = (unsigned short *)foo();
/* do something with *dptr... ? */
printf ("dptr -> 0x%04hX\n", *dptr);
return 0;
}
---END---
Look mah, no warnings. Its still blatantly wrong. (big endian
machines should give you "dptr -> 0x0000", little endian machines
should give you "dptr -> 0x1234", thus illustrating how wrong this
is.)
I don't care if you were taught at Bell Labs, it certainly isn't
showing now.
C.
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html