1. Home
  2. Computer Science MCQs
  3. C Programming
  4. With int a[4] = {10, 20, 30, 40}; int *p = a; what does…

Added 30 Nov -0001

With int a[4] = {10, 20, 30, 40}; int *p = a; what does printf("%d", *(p + 2)); print?

  1. 20, the array's second element
  2. 10, the element at the base address
  3. The address of the third element itself
  4. 30, the third element of the array Correct answer

Correct answer

D. 30, the third element of the array

12 views

MCQSEXAM

Explanation

Pointer arithmetic is scaled by the size of the pointed-to type, so p + 2 points two ints past the base, and the dereference yields a[2], which is 30. p + 2 is not two bytes ahead unless the element type were char. Because a[i] is defined as *(a + i), the expression could equally be written p[2], and both forms stay in bounds only while the offset is less than 4.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs