Here is the code
int main()
{
char *p;
char a[]={"Hello"};
(p=a)[0]; // This line
return 0;
}
Is the behaviour of this code undefined in C++?
Yes, AFAIK. There is no intervening sequence point between the
assignment and indexing. Even though there are parentheses, it is not
guaranteed that the compiler isn't going to "optimize" it and use the
previous value of 'p' (undefined) for dereferencing it.
I think now that I was wrong there. The expression in parentheses has
the value the same as 'a', and *that* is used to dereference the pointer
(the indexing is adding the index and then dereferencing). The relevant
portion of the Standard (using the latest Draft):
<<The assignment operator (=) and the compound assignment operators all
group right-to-left. All require a modifiable lvalue as their left
operand and return an lvalue referring to the left operand. The result
in all cases is a bit-field if the left operand is a bit-field. In all
cases, the assignment is sequenced after the value computation of the
right and left operands, and before the value computation of the
assignment expression.>>
The last sentence is important, I believe. "Sequenced" is the key word..
> If you need to
introduce a sequence point, use the comma:
V