fork download
  1. #include <iostream>
  2.  
  3. // This is the correct declaration of the parameter
  4. int my_strlen(const char *s)
  5. {
  6. // Which requires const char* here:
  7. const char *p = s;
  8.  
  9. // 7_const.cpp: In function `int my_strlen(const char*)':
  10. // 7_const.cpp:8: assignment of read-only location
  11. while ( ! (*p = 0) ) ++p;
  12. return p - s;
  13. }
  14.  
  15. int main()
  16. {
  17. char t[] = "Hello";
  18. std::cout << my_strlen(t) << std::endl; // t converted to const char *
  19. return 0;
  20. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int my_strlen(const char*)':
prog.cpp:11:19: error: assignment of read-only location '* p'
     while ( ! (*p = 0) ) ++p;
                   ^
stdout
Standard output is empty