& operator

Morris Keesan keesan at bbncca.ARPA
Sat Dec 17 00:56:38 AEST 1983


-------------------------------
Dave Olson (fortune!olson) says:

>I would have to disagree with chris as to what should be assigned by:
>	 char *in, *out, buf[];
>	 in = buf;
>	 out = &(*in++);
>since he forgot the parentheses.  Given the parentheses, out SHOULD be
>set buf+1; that is, 'in' is incremented BEFORE the & operator is applied.

I would have to disagree.  Extending the parenthesization one step further, we
get the following three equivalent statements:
	out = &*in++;
	out = &(*in++);
	out = &(*(in++));   /* ++ and unary * and & group right to left */

    (in++) is the value of 'in' BEFORE it is incremented.  Once this value is
evaluated, 'in' can be incremented, but its new value has no effect on the
further evaluation of the expression.  Then (*(in++)) is the object pointed to
by 'in' before incrementation, and &(*(in++)), or (&*in++), is its address,
which is of course the original value of 'in', which is 'buf', not 'buf+1'.
-- 
					Morris M. Keesan
					{decvax,linus,wjh12}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA



More information about the Comp.lang.c mailing list