Mail Archives: djgpp/2000/05/24/09:45:29
Waldemar Schultz wrote:
>RadSurfer schrieb:
>> Do I have to resort to a routine to emulate strrev() ?
To be portable, yes. You should then also give it a different
name, because all names starting with str and followed by a
lower case letter, are reserved.
>#include <string.h>
>
>#if defined(__cplusplus) && __cplusplus
> extern "C" {
>#endif
>char *strrev(char *str)
>{
>char *p1=NULL,*p2=NULL;
Minor point: initialization is not needed here.
> if(! str || ! *str)
> return str;
> for(p1=str,p2=str+strlen(str)-1;p2>p1;*p1^=*p2,*p2^=*p1,
> *(p1++)^=*(p2--));
This is the reason for my followup. Why do you obfuscate the code,
by using the xor trick for swapping, and by doing all the work
in the for.
The xor-trick will produce slower code on x86 (and probably most
architectures). It will need three XORs and three MOVs, compared
to four MOVs by the straightforward swapping code. It will need
one less register with current gcc, though.
Also, using straightforward swapping code has the advantage,
that you don't have to think about problems with binary operations,
when char is signed. But that of course is a language lawyer issue.
> return str;
>}
--
Regards, Dieter
- Raw text -