int* p = 0;
int* q = &*p;
Is this undefined behavior or not? I browsed some related questions, but this specific aspect didn’t show up.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The answer to this question is: it depends which language standard you are following :-).
In C90 and C++, this is not valid because you perform indirection on the null pointer (by doing
*p), and doing so results in undefined behavior.However, in C99, this is valid, well-formed, and well-defined. In C99, if the operand of the unary-
&was obtained as the result of applying the unary-*or by performing subscripting ([]), then neither the&nor the*or[]is applied. For example:Likewise,
From C99 §6.5.3.2/3:
(and its footnote, #84):