我正在努力理解下面的代码,它用于反向以空字符结尾的字符串。我写了评论,解释了我正在努力理解的内容。了解反向字符串的代码
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