// (C) Porkolab 2003 // // A.1.3. // Constants to detect errors #include <iostream> static int my_strlen(char *s) { char *p = s; // here the programmer has made fatal error while ( ! (*p = 0) ) ++p; return p - s; } // This program likely cause run-time error int main() { char t[] = "Hello"; std::cout << my_strlen(t) << std::endl; return 0; } ///////////////////////////////////////////////////////////////// #include <iostream> // this is the correct declaration of parameter static int my_strlen(const char *s) { // ... which requires const char* here: const char *p = s; // 7_const.cpp: In function `int my_strlen(const char*)': // 7_const.cpp:8: assignment of read-only location while ( ! (*p = 0) ) ++p; return p - s; } int main() { char t[] = "Hello"; std::cout << my_strlen(t) << std::endl; return 0; }