Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!bellcore!savage!garnett From: garnett@savage.bellcore.com (Michael Garnett) Newsgroups: comp.lang.c++ Subject: Re: const vs. void Message-ID: <1991Mar21.192033.6032@bellcore.bellcore.com> Date: 21 Mar 91 19:20:33 GMT References: <1991Mar20.164518.19928@intelhf.hf.intel.com> Sender: usenet@bellcore.bellcore.com (Poster of News) Reply-To: garnett@thumper.bellcore.com Organization: Information Networks Research (Bellcore) Lines: 41 In article <1991Mar20.164518.19928@intelhf.hf.intel.com>, ajw@watson.hf.intel.com (Alan Waldock) writes: |> This is a kind of repost, on account of our mail servers are up |> the creek. |> |> If, to protect data, I point to it with a 'char const *', the |> compiler prevents me from using that pointer as the first (destination) |> argument to 'strncpy()', which requires a 'char *'. |> |> However, I'm free to pass it (as destination) to 'memcpy()', which |> specifies a 'void *' as its first argument. |> |> It's as if a 'void *' will accept any pointer, const or no. |> Is this right? |> |> -- Alan Waldock, from but not on behalf of Intel Corporation |> ajw@watson.hf.intel.com ...uunet!intelhf!watson!ajw --------- ]]]]] I never use "char const *", but I tested it and it seems to behave like ]]]]] "const char *". I assume that this is correct. Can anyone verify this? You compiler is right to complain about the call to strncpy. If that pointer is passed into strncpy, strncpy can change your pointed-to data (that is what strncpy is supposed to do to its first argument). However, allowing you to pass it into a "void *" is WRONG. You CAN pass it into a "const void *" ("void const *"?). The gist is... you can't "protect" your pointed-to data.. and still expect functions like strncpy to be able to change the data for you. You can cast away the constness as in strncpy ((char*)foo, bar, 5) BUT this is strongly NOT advised. ------------------------------------------------------------------------ ********* Michael S. Garnett ** o ** Information Networks Research ** _- -_ ** Ph: 201-829-4591 ** / \ ** Internet: garnett@thumper.bellcore.com * | | * UUCP: ...!bellcore!thumper!garnett ** | | ** ** /_______\ ** // I speak for only myself, and NOT any company or ** o ** // organization directly or indirectly associated with ********* // Bell Communications Research