c++ - Is pointer valid condition for a loop? -
can explain, why in case loop repeats 10 times?
#include <iostream> using namespace std; int main(){ int length=-1; char s[]="bad or nice";//10 characters ( char *q = s; *q; ++q ){ cout << *q; length++; } cout << "\n" << length; return 0; }
the condition value @ memory location of pointer (*q).
therefore loop terminates when null character reached (at end of string), evaluated false.
Comments
Post a Comment