All mock tests are FREE till 31 December — register to save your progress

What is the output of the following code?
main() {
  struct s1 { char *z; int i; struct s1 *p; };
  static struct s1 a[] = {
    {"Nagpur", 1, a+1},
    {"Raipur", 2, a+2},
    {"Kanpur", 3, a}
  };
  struct s1 *ptr = a;
  printf("%s %s %s\n", a[0].z, ptr->z, a[2].p->z);
}

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: B. Nagpur Nagpur Nagpur

Explanation

a[0].z is 'Nagpur'. ptr points to a[0], so ptr->z is 'Nagpur'. a[2].p points back to a, so a[2].p->z is also 'Nagpur'.

More Programming Languages and Computer Graphics questions