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

david.f.prosser dfp at cbnewsl.ATT.COM
Fri Aug 25 08:09:59 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''.

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...



More information about the Comp.lang.c mailing list