Question about math.pi is mutable

Marko Rauhamaa marko at pacujo.net
Sun Nov 8 02:45:55 EST 2015


Grant Edwards <invalid at invalid.invalid>:

> On 2015-11-07, Marko Rauhamaa <marko at 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.

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.


Marko



More information about the Python-list mailing list