Hi, in the below given program the answer is
1. agpur
2. jabalpur
3. jabalpur
I understood the processing for the first two printf statements but for the last printf statement I could not understand the processing. Could anyone please explain.
Thanks in advance :)

#include<stdio.h>
#include<conio.h>

void main()
{
	struct s1
	{
		char*z;
		int i;
		struct s1 *p;
	};

 static struct s1 a[]={{"Nagpur",1,a+1},{"Raipur",2,a+2},{"Jabalpur",3,a}};

 struct s1 *ptr=a;
 clrscr();
 printf("%s \n",++(ptr->z));
 printf("%s \n", a[(++ptr)->i].z);
 printf("%s\n",a[--(ptr->p->i)].z);

 getch();
 }

This would be:

ptr->p->i = 3;
So, --(ptr->p->i) = 2;

So, a[2].z is what is being printed. Which is, "Jabalpur".

Still i couldn't get it , Could you please elaborate this line

ptr->p->i = 3;

ptr is incremented in line number 18. So, ptr points to a + 1. So, ptr->p is a + 2.
Therefore, ptr->p->i = *(a + 2).i

Got it. Thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.