I want to use pointers to reverse a char array in C++. I was wondering if there is anything that I should do differently? Am I doing this correctly? Is there a more efficie
You could use std::swap(*pStart, *pEnd)
instead of open-coding swap.
Heck, you could just use std::reverse(buffer, buffer + strlen(buffer))
. But I suppose that wouldn't really be using pointers yourself, and given that requirement, it looks fine.
Well, actually, a tiny nit: if length==0
, then &string[length - 1]
isn't pointing into the character array and is theoretically not a valid pointer.