Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.std.c Subject: Re: Return pointers-to-const where data shall not be modified Date: Wed, 06 May 2020 13:18:52 -0700 Organization: None to speak of Lines: 43 Message-ID: <87tv0szuj7.fsf@nosuchdomain.example.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="dcfcb51887ac7802667ee20ddc196f88"; logging-data="2300"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+X2T7lS90ZTSCVlgApCULJ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:urkW2dfzP4Exd5VXdvTgjS9fz3E= sha1:wkQnMMu4WK1QzBrV4F7s95N+V7w= Xref: csiph.com comp.std.c:6082 Philipp Klaus Krause writes: > There are 4 functions (getenv, localeconv, setlocale, strerror) in the > standard library that return a pointer and state that the return value > points to something that "shall not be modified by the program". I > suggest to make these functions return pointers-to-const, e.g. > > change > > char *strerror(int errnum); > > to > > const char *strerror(int errnum); > > A draft proposal is at > http://www.colecovision.eu/stuff/proposal-const-shall-not-be-modified-stdlib.html The formatting of the last paragraph on that web page could be improved. > Do you see any problem with this change? It would likely break existing code, for the same reason that making string literals const would break existing code. Any program that has something like: char *username = getenv("USER"); would have to be modified. That would include any pre-ANSI code that calls getenv. (C++ has made some tweaks to some C standard library functions, and it *could* have modified getenv in this way, but it hasn't.) > I was wondering if it make sense to also change the char * members of > struct lconf to const char *, but I fear that change might break more stuff. I think you mean lconv, and that would likely present the same problem. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */