ProgrammingHardTCS NQT · Infosys pattern

Assume no padding is needed. What is printed?

Assume no padding is needed. What is printed?
#include <stdio.h>
struct P { int x; int y; };
int main(void) {
    struct P arr[3];
    struct P *p = arr;
    printf("%ld", (long)(p + 1) - (long)p);
}
  1. A. 4
  2. B. 16
  3. C. 8
  4. D. 2

Correct answer: C. 8

Pointer arithmetic on a struct P* advances by sizeof(struct P) bytes. With two 4-byte ints and no padding needed, sizeof(struct P) is 8.

More C Programming questions

Exams that ask this topic