Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Question about math.pi is mutable Date: Mon, 9 Nov 2015 14:49:15 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 52 Message-ID: References: <87d1vlzy4p.fsf@elektro.pacujo.net> <878u69zxww.fsf@elektro.pacujo.net> <87y4e9y9j6.fsf@elektro.pacujo.net> <87d1vlx7bg.fsf@elektro.pacujo.net> NNTP-Posting-Host: c-24-118-110-103.hsd1.mn.comcast.net X-Trace: reader1.panix.com 1447080555 18145 24.118.110.103 (9 Nov 2015 14:49:15 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 9 Nov 2015 14:49:15 +0000 (UTC) User-Agent: slrn/1.0.2 (Linux) Xref: csiph.com comp.lang.python:98528 On 2015-11-08, Marko Rauhamaa wrote: > Grant Edwards : > >> On 2015-11-07, Marko Rauhamaa 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 > #include > > 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