Machine-translated by Google. Questions are authored in English.
Top

Discussion

If int *p points to arr[2], then (p+1) points to

  • A.arr[1]
  • B.arr[3]
  • C.arr[2]+1
  • D.Cannot determine

Answer: B

p + 1 points to arr[3].

In pointer arithmetic, adding 1 to a pointer moves it forward by one element of the type it points to, not by one byte. If p holds the address of arr[2], then p + 1 holds the address of the next int, arr[3]; the compiler scales the 1 by sizeof(int) automatically.

arr[1] would be p - 1. arr[2]+1 describes adding 1 to the value stored in arr[2], not moving the pointer, and the result can be determined exactly from the rules of pointer arithmetic.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.