Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!ames!ptsfa!ihnp4!homxb!mtuxo!mtune!lzaz!lznv!psc From: psc@lznv.ATT.COM (Paul S. R. Chisholm) Newsgroups: comp.lang.c Subject: const * question Message-ID: <1151@lznv.ATT.COM> Date: Sat, 17-Oct-87 04:24:03 EDT Article-I.D.: lznv.1151 Posted: Sat Oct 17 04:24:03 1987 Date-Received: Sun, 18-Oct-87 11:32:44 EDT Organization: AT&T Lines: 40 Keywords: const pointer I really like the idea of const pointers. I think I even understand them: "const char *p" is a pointer to a constant string, "char *const p" is a constant pointer to a string, and "const char *const p" is a constant pointer to a constant string. But I've stumbled a case where either I'm confused, or my compiler is. Please consider the following routine: char * next( const char *s ) { const char *p; p = s + 1; return p; } The function next() is not allowed to modify the contents of the string passed to it. (It's irrelevant whether it changes s or not. s is a function argument, and thus, just a copy of the pointer.) It's appropriate for p to be a pointer to constant, too, since it points to (some of) the same data. Here's the rub: my compiler (Turbo C) complains that the return statement is a suspicious pointer conversion. Well, I can understand that, sort of: it's converting from a const char* to a char*, and most of the time, that's an attempt to modify constant data you're passed a pointer to. But it's not appropriate for next() to return (const char*); after all, the caller may very well be allowed to modify the data it passed a pointer to! Have I missed an important point? Have the compiler writers confused return conversion semantics with assignment return semantics in this case? Which needs the fix: my code, or their compiler? -Paul S. R. Chisholm, {ihnp4,cbosgd,allegra,rutgers}!mtune!lznv!psc AT&T Mail !psrchisholm, Internet psc@lznv.att.com I'm not speaking for my employer, I'm just speaking my mind.