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 ",++(ptr->z));
	printf("%s ",a[(++ptr)->i].z);
	printf("%s",a[--(ptr->p->i)].z);
}

 

Answer

ptr->z points to "Nagpur", and incrementing it will point to the next character of the string, so first printf will write "agpur". Next, incrementing ptr by 1 will point to a[1], so (++ptr)->i gives 2 and a[(++ptr)->i].z i.e. a[2].z gives "Kanpur". Now, ptr points to a[1], so ptr->p gives a+2, i of a[2] is 3. So, a[--3].z i.e. a[2].z again gives us "Kanpur". Output of the program is "agpur Kanpur Kanpur".

0Comment