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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

c# - How do I get the Nth largest element from a list with duplicates, using LINQ? -

jsp - "Sending a redirect is forbidden after the response has been committed" in sendRedirect -