Path: utzoo!attcan!uunet!wuarchive!brutus.cs.uiuc.edu!apple!rutgers!att!cbnewsl!dfp From: dfp@cbnewsl.ATT.COM (david.f.prosser) Newsgroups: comp.lang.c Subject: Re: Is ``char c; strcpy(&c,"");'' ANSI C? Message-ID: <1619@cbnewsl.ATT.COM> Date: 24 Aug 89 22:09:59 GMT References: <30@looney.twinsun.com> Reply-To: dfp@cbnewsl.ATT.COM (david.f.prosser,sf,) Organization: AT&T Bell Laboratories Lines: 46 In article <30@looney.twinsun.com> eggert@twinsun.com (Paul Eggert) writes: >Suppose we have ``strcpy'' as defined in K&R 5.5, p.106: > > void strcpy(char *s, char *t) { while (*s++ = *t++) ; } >Then > ... char c; strcpy(&c, ""); ... (1) > >is not ANSI C, because strcpy calculates (&c) + 1, violating K&R A7.6, p.205, >which says pointer arithmetic can be applied only to a ``pointer to an object >in an array''. While K&R II is a good way to learn ANSI C, it cannot take the place of the actual standard. Section 3.3.6, page 48, lines 9-11: For the purposes of these operators [binary + and -], a pointer to a nonarray object behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type. The rest of that section notes that it is valid to point "one past" the last element of an array. Thus char c; ... (&c + 1) ... is valid, as long as the address is not dereferenced. Section 4.1.6, page 100, lines 5-9: If a function argument is described as being an array, the pointer actually passed to the function shall have a value such that all address computations and accesses to objects (that would be valid if the pointer did point to the first element of such an array) are in fact valid. This is part of the introduction to the entire library section of the standard. Section 4.11.2.3, page 163, lines 36-37: The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. Similar descriptions are used for other functions. Dave Prosser ...not an official X3J11 answer...