LearnFrenzy
Sign in Take a free test

Pointers · Predict the Output · Question 2

Output of pointer arithmetic on an array in C Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[] = {10, 20, 30, 40, 50};
    int *p = a;
    p += 2;
    printf("%d %d\n", *p, *(p + 1));
    return 0;
}
  • A 12 13
  • B 30 40 Correct
  • C 20 30
  • D 30 31
Correct Answer: Option B

p starts at a[0]. p += 2 moves it two elements to a[2] (30), and *(p + 1) is a[3] (40). Pointer arithmetic counts elements, not bytes.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Popular