ProgrammingVery hard
Two pointers p and q point into the same block allocated with malloc(10 * sizeof(int)). Which subtraction is defined?
- A. p - q, when p points into one block and q into another
- B. (char *)p - (char *)q, when one points past the end by more than one element
- C. p - q, when both point into that block
- 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
- A linked list of length n is reversed in place with a loop. How much extra memory is used?
- unsigned int h = 0; for (const char p = "ab"; p; p++) h = h 31 + p; printf("%u", h); What is printed (ASCII, 32-bit unsigned)?
- unsigned int x = 0b11011010; printf("%u", (x >> 3) & 7); What is printed?
- With 32-bit unsigned arithmetic, what does (unsigned)-5 >> 1 print?
- What is the value of ~5 & 0xF in C, with int of 32 bits?
- What is the value of (3 ^ 5) & ~1 in C?