我正在努力理解下面的代碼,它用於反向以空字符結尾的字符串。我寫了評論,解釋了我正在努力理解的內容。瞭解反向字符串的代碼
void reverse(char *str)
{
char *end = str;
char tmp;
if (str) // I don't understand what is happening within this if loop and how this is carried out for `str` which is not Boolean
{
while (*end) // I dont know what this means either
{
++end;
}
--end;
while (str < end)
{
tmp = *str
*str++ = *end //I am confused as to why this is not just *str = *end
*end-- = tmp; //Similarly, why is this not *end = tmp
}
}
}
@JamesAdkison我在第二個while循環中留下了額外的縮進以顯示代碼缺少分號。 – NathanOliver
@NathanOliver如果它不會引起分段錯誤,那將是一個非常惡劣的錯誤。 –
@ManosNikolaidis是的。我只是不想修改OP的代碼,但修正了縮進,因爲它在發佈時是平坦的。 – NathanOliver