vendredi 20 février 2015

Reversing a string in C using pointers - purpose of if statement?

In the code below that reverses a string, I don't understand what is the purpose of the if statement. What case is this if statement trying to catch for?


Isn't this superfluous since you've already set char*end= str, and so the statement if(str) has to be true or your code would have already failed by this point?



void reverse(char* str)
{
char *end = str;
char temp;


if(str){
while (*end)
{
end++;
}
}

end--; //pulls back one for \0 character

while (str < end)
{
temp = *str;
*str++ = *end;
*end-- = temp;
}

}

Aucun commentaire:

Enregistrer un commentaire