我正在研究“破解编码面试”中的一个问题。问题是:编写代码以反转C样式字符串。这是我用c ++编写的答案,但是我不明白增加char指针的含义。
void swap(char &a, char &b){ a = a^b; b = a^b; a = a^b; } void reverse1(char *s){ if(!s) return; char *p = s, *q = s; while(*q) ++q; --q; while(p < q) swap(*p++, *q--); }
我不了解的部分在第一个while循环的第二个函数中。什么情况*p?以及为什么增量q会截断字符串中的起始字符?
例如,如果
char s[] = "1234567890";
当您s进入功能后reverse1,为什么++q会给您类似的结果“234567890”?