I understand that we want to use assert
when we want to check for impossible case. In a book I have, I saw the following two examples:
void foo(char* str) { assert ((str+strlen(str)) != NULL); }
void bar(char* str) { assert (str[strlen(str)] != NULL); }
I'm trying to figure out if they are valid checks. As I understand the first example is not valid because we check if the address is NULL
and that is not right. But I'm not sure what we are checking in the second example. I'm having hard time understanding when its the right time to use assert
. I understand the explanation of "checking impossible cases" but it a bit different for each case.
EDIT: Is there a diffrence between the functions I showed before and those?:
void foo(char* str) { assert (!(str+strlen(str))}
void bar(char* str) { assert (! str[strlen(str)]); }