#include <iostream>
using namespace std;
size_t strlen(const char *s){
const char *sc;
for(sc = s; *sc != '\0'; ++sc)
return (sc-s);
}
void bad_func( const char * s)
{
const char * sc = s;
*sc = 'a'; // error, modifying a const char through sc
// trying to subvert the const-ness with a non-const pointer will fail too
char * s2 = s; // error, cannot convert const char* to char*;
}
int main() {
// your code goes here
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc2l6ZV90IHN0cmxlbihjb25zdCBjaGFyICpzKXsKICAgY29uc3QgY2hhciAqc2M7CiAgIGZvcihzYyA9IHM7ICpzYyAhPSAnXDAnOyArK3NjKQoKICAgcmV0dXJuIChzYy1zKTsgIAp9Cgp2b2lkIGJhZF9mdW5jKCBjb25zdCBjaGFyICogcykKewoJY29uc3QgY2hhciAqIHNjID0gczsKCSpzYyA9ICdhJzsgLy8gZXJyb3IsIG1vZGlmeWluZyBhIGNvbnN0IGNoYXIgdGhyb3VnaCBzYwoJCgkvLyB0cnlpbmcgdG8gc3VidmVydCB0aGUgY29uc3QtbmVzcyB3aXRoIGEgbm9uLWNvbnN0IHBvaW50ZXIgd2lsbCBmYWlsIHRvbwoJY2hhciAqIHMyID0gczsgLy8gZXJyb3IsIGNhbm5vdCBjb252ZXJ0IGNvbnN0IGNoYXIqIHRvIGNoYXIqOwp9CgppbnQgbWFpbigpIHsKCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKCXJldHVybiAwOwp9
prog.cpp: In function ‘void bad_func(const char*)’:
prog.cpp:14:8: error: assignment of read-only location ‘* sc’
*sc = 'a'; // error, modifying a const char through sc
^~~
prog.cpp:17:14: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
char * s2 = s; // error, cannot convert const char* to char*;
^