Possible Duplicate:
Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?
int array[10];
int* a = array + 10; // well-defined
int* b = &array[10]; // not sure...
Is the last line valid or not?
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.
Yes, you can take the address one beyond the end of an array, but you can’t dereference it. For your array of 10 items,
array+10would work. It’s been argued a few times (by the committee, among others) whether&array[10]really causes undefined behavior or not (and if it does, whether it really should). The bottom line with it is that at least according to the current standards (both C and C++) it officially causes undefined behavior, but if there’s a single compiler for which it actually doesn’t work, nobody in any of the arguments has been able to find or cite it.Edit: For once my memory was half correct — this was (part of) an official Defect Report to the committee, and at least some committee members (e.g., Tom Plum) thought the wording had been changed so it would not cause undefined behavior. OTOH, the DR dates from 2000, and the status is still “Drafting”, so it’s open to question whether it’s really fixed, or ever likely to be (I haven’t looked through N3090/3092 to figure out).
In C99, however, it’s clearly not undefined behavior.