Hello, I'm a highly experienced expert C programmer
Apparently, you are
a) egotistical, and
b) wrong
and I've written
this code to reverse a string in place.
I am /so/ sorry, both for you and your employer.
I think you could all learn
something from it!
I'm sure that you are right. After all, /I/ learned that you are much less
than the C programmer that you claim to be.
int reverse(char* reverseme){
int retval=-1;
if(retval!=NULL)
Useless test, as retval was initialized (and never altered) to a value
that will not equate to NULL
First real failure. retval is an integer, and the argument to
strlen() is a pointer-to-char. You can't get a valid result from
taking the "string length" of an integer.
Second failure: you did not #include the header that declares the
strlen() function. Thus, your compiler did not flag the above
assignment as an error (argument of incompatable type)
So, what is the "string length" of an integer? And when would it be
greater than zero?
Style hack: To be clearer to the intent of what sort of number
half represents, you should have coded this as
int half = len / 2;
Of course, since len is a nonsense value, half will also be a
nonsense value.
for(;retval<half;retval++){
reverseme[retval]^=reverseme[len-(retval+1)];
Undefined behaviour on first iteration, when retval == -1
reverseme[-1] is out of bounds.
reverseme[len-(retval+1)]=reverseme[retval];
Undefined behaviour on first iteration - same reason
reverseme[retval]^=reverseme[len-(retval+1)];
Undefined behaviour on first iteration - same reason
Plus, you've screwed up the "xor trick", and wiped out /both/
ends of the string with invalid values.
The head end now contains an array of 0x00 (up to
the ill-computed "midpoint"), while the tail end contains the
results of the head end xor the tail end.
Of what use is this return value?
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------