ProgrammingVery hard

Two pointers p and q point into the same block allocated with malloc(10 * sizeof(int)). Which subtraction is defined?

  1. A. p - q, when p points into one block and q into another
  2. B. (char *)p - (char *)q, when one points past the end by more than one element
  3. C. p - q, when both point into that block
  4. D. p - q, when one pointer is NULL

Correct answer: C. p - q, when both point into that block

Pointers within the same array (or one past its end) can be subtracted. Subtraction across unrelated blocks or with NULL is undefined.

More C Programming questions

Exams that ask this topic