Path: utzoo!attcan!uunet!cs.utexas.edu!csd4.csd.uwm.edu!uxc.cso.uiuc.edu!tank!shamash!nic.MR.NET!thor.acc.stolaf.edu!mike From: mike@thor.acc.stolaf.edu (Mike Haertel) Newsgroups: comp.lang.c Subject: Re: Is ``char c; strcpy(&c,"");'' ANSI C? Message-ID: <4949@thor.acc.stolaf.edu> Date: 25 Aug 89 04:05:25 GMT References: <30@looney.twinsun.com> <1619@cbnewsl.ATT.COM> Reply-To: mike@thor.stolaf.edu (Mike Haertel) Organization: St. Olaf College, Northfield, MN Lines: 33 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''. In addition to the various people who have posted pointing out that every object is in effect an array of one element and &c + 1 is therefore sanctioned I should like to point out that just because certain implementations of strcpy happen to compute &c + 1, doesn't mean it's part of the language that strcpy would compute &c + 1. All too often people in this newsgroup are confusing details of their implementation with the defined behavior of the language. /* strcpy that doesn't compute &c + 1 */ char * strcpy(char *s, const char *t) { char *r = s, c; while (c = *t) *s++ = c; return r; } -- Mike Haertel ``There's nothing remarkable about it. All one has to do is hit the right keys at the right time and the instrument plays itself.'' -- J. S. Bach