Is ``char c; strcpy(&c,"");'' ANSI C?

Mike Haertel mike at thor.acc.stolaf.edu
Fri Aug 25 14:05:25 AEST 1989


In article <30 at looney.twinsun.com> eggert at 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 <mike at stolaf.edu>
``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



More information about the Comp.lang.c mailing list