on-internet
← Community

Pointer-to-array overlapping end of array

Stack Overflow ·

Is this code correct? int arr[2]; int (*ptr)[2] = (int (*)[2]) &arr[1]; ptr[0][0] = 0; Obviously ptr[0][1] would be invalid by accessing out of bounds of arr. Note: There's no doubt that ptr[0][0] designates the same memory location as arr[1]; the question is whether we are allowed to access that memory location via ptr. Here are some more examples of when an expression does designate the same memory location but it is not permitted to access the memory location that way. Note 2: Also consider **ptr = 0; . As pointed out by Marc van Leeuwen, ptr[0] is equivalent to *(ptr + 0), however ptr + 0

Comments

Sign in or sign up to comment and upvote.

No comments yet.