I got a comment about my code that it was C99. Just for my own understanding, can someone clear up for me what's C99 about it? Is it the way I declared strarray? I really should get my hands on some standards books
Thanks
I got a comment about my code that it was C99. Just for my own understanding, can someone clear up for me what's C99 about it? Is it the way I declared strarray? I really should get my hands on some standards books
Thanks
If you understand what you're doing, you're not learning anything.
Here's another one. It uses the XOR trick, but you can use another temporary variable to swap characters.
PS: itsme86: Yes, it's the way you declared strarray (array size computed in run-time).Code:char *revstr(char *const string) { register char *begin; register char *end; const size_t length = strlen(string); for (begin = string, end = string + length - 1; begin < end; begin++, end--) *begin ^= *end ^= *begin ^= *end; return string; }
"In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton
You know there's really no point in ever using the 'register' keyword, right?
Quzah.
Hope is the first step on the road to disappointment.
> *begin ^= *end ^= *begin ^= *end;
Oh please, not this nonsense again!!!!
http://www.eskimo.com/~scs/C-faq/q10.3.html
Pay REALLY close attention now - read the bit which says
"and the ``obvious'' supercompressed implementation for integral types a^=b^=a^=b is illegal due to multiple side-effects; see question 3.2)."
I seem to remember a post of mine some time ago which showed that the compiler was way smarter than the "clever" programmer in that the blindingly obvious "use a temporary variable" actually produced the smallest code, and that all these arithmetic and logical tricks produced longer instruction sequences.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Originally Posted by itsme86
Variable-size arrays in C have always been (pre-C99) a no-no:
(You do realize, don't you, that there's a little elf ---near the East Pole--- who collects a royalty of .01 euros for every use of malloc() in a commercial program? If everybody started using variable-length arrays, he would have to declare bankruptcy. The next thing you know, he would given his own reality-TV show. Too horrible to imagine!)Code:void revstr(char *str) { struct str_s { char ch; char *ptr; } strarray[strlen(str)], temp;
Regards,
Dave
Last edited by Dave Evans; 02-17-2005 at 01:37 PM.