On Wed, 23 Jul 2003, Naren wrote:
"Nick Austin" <ni**********@nildram.co.uk> wrote... On Wed, 23 Jul 2003 16:43:11 +0530, "Naren" wrote...
main()
{
short int i=2;
i = hello(&i);
i = hello(&i);
printf("%d",i);
}
This works fine but I thought of replacing the two calls to hello
By one call
i = hello(&(i = hello(&i)));
i = hello( (i = hello(&i), &i) );
This is too good.but could you explain to me why mine went wrong?
Yours tries to take the address of (i = hello(&i)), which is just
silly. (i = ...) is an expression, not an lvalue - it's like trying
to take the address of (i - 1), or (42). You just can't do it in
the C programming language, because the expression's value doesn't
necessarily have to "exist" anywhere that has an address to be
taken!
Nick's fix uses the "comma operator", which is *completely different*
from the commas used to separate function arguments. The comma operator
introduces a "sequence point" between the expression (i = hello(&i))
and the expression (&i), and returns the value of the second expression
(&i). That's the value that gets passed to 'hello' the second time.
Sequence points should be covered in the FAQ; search Google for
"comp.lang.c FAQ".
HTH,
-Arthur