Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!uwvax!rutgers!ames!oliveb!sun!gorodish!guy From: guy%gorodish@Sun.COM (Guy Harris) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: pointer alignment when int != char * Message-ID: <26843@sun.uucp> Date: Sat, 29-Aug-87 03:16:30 EDT Article-I.D.: sun.26843 Posted: Sat Aug 29 03:16:30 1987 Date-Received: Sun, 30-Aug-87 08:47:07 EDT References: <493@its63b.ed.ac.uk> <6061@brl-smoke.ARPA> <3812@spool.WISC.EDU> <2130@umn-cs.UUCP> Sender: news@sun.uucp Followup-To: comp.lang.c Lines: 29 Xref: mnetor comp.lang.c:4024 comp.unix.wizards:3969 > How does anything know if s & c are pointing to members of the same array? > If s isn't 0 terminated, c could end up anywhere... If "s" isn't 0 terminated, the result returned from "strlen" isn't meaningful anyway! As such, the fact that "c" might not be in the same array is hardly relevant. The rules don't say that the implementation MUST detect whether the two pointers belong to the same array, and slap your wrists if they aren't; they say that the behavior is *undefined* if the pointers aren't members of the same array! As such, nobody *has* to know if "s" and "c" are pointing to members of the same array. In any *valid* call to "strlen", the pointers will be members of the same array: 1) It could be a string constant, which is an array; 2) It could be an object declared as an array; 3) It could be an array allocated by "malloc", or an array that is a component of an object allocated by "malloc". In all *these* cases, if the array contains a valid string, the call to "strlen" must return a meaningful result, and your sample code for "strlen" will subtract two pointers that point to members of the same array, or a pointer that points to a member of an array from a pointer one past the end of that array, both of which are valid. Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com