On 2015-11-08, Marko Rauhamaa <ma...@pacujo.net> wrote: > Grant Edwards <invalid@invalid.invalid>: > >> On 2015-11-07, Marko Rauhamaa <ma...@pacujo.net> wrote: >>> "const" is a very ineffective tool that clutters the code and forces >>> you to sprinkle type casts around your code. >> >> But it allows the compiler to warn you if you pass a pointer to a >> read-only data to a function that expects a pointer to writable data. > > Unfortunately, it doesn't: > >======================================================================== > #include <stdio.h> > #include <string.h> > > int main() > { > const char name[] = "Tom"; > char *p = strstr(name, "Tom"); > strcpy(p, "Bob"); > printf("name = %s\n", name); > return 0; > } >======================================================================== > > $ cc -o prog prog.c > $ ./prog > Bob > > No warning.
Yes, there are ways to fool the compiler by converting a const char* to a plain char* like you did with strstr(). But in my experience there are plenty of cases where it will generate a useful warning. > Point is, the consequences of "proper" use of const are so annoying > even standard library functions would rather grossly abuse it than > tolerate compiler warnings everywhere. That's your opinion. My differs: _I_ find it useful. I don't have that many problems with it. If you don't like it, don't use it -- nobody is forcing you to declare 'const' variables. -- Grant -- https://mail.python.org/mailman/listinfo/python-list