What is the output of the following program? main() {

What is the output of the following program?

main()
{
	struct s1
	{
		char *z;
		int i;
		struct s1 *p;
	};
	struct s1 a[] = {{"Nagpur",1,a+1},{"Raipur",2,a+2},{"Kanpur",3,a}};
	struct s1 *ptr = a;
	printf("%s %s %s",a[0].z,ptr->z,a[2].p->z);
}

 

Answer

First, a[0].z contains "Nagpur". As ptr contains the base address of a, ptr->z will also give us "Nagpur". Next, a[2].p contains a. So, a[2].p->z also means "Nagpur". Output of the program is "Nagpur Nagpur Nagpur".

0Comment