C Programming - Complicated Declarations - Discussion
Discussion Forum : Complicated Declarations - Find Output of Program (Q.No. 10)
10.
What will be the output of the program?
#include<stdio.h>
int main()
{
struct s1
{
char *z;
int i;
struct s1 *p;
};
static struct s1 a[] = {{"Nagpur", 1, a+1} , {"Chennai", 2, a+2} ,
{"Bangalore", 3, a} };
struct s1 *ptr = a;
printf("%s,", ++(ptr->z));
printf(" %s,", a[(++ptr)->i].z);
printf(" %s", a[--(ptr->p->i)].z);
return 0;
}
Discussion:
10 comments Page 1 of 1.
Rohan Sanyal said:
1 decade ago
*ptr=a=a[0];
Therefore, ptr->z => Nagpur=> ++ptr->z = agpur.
Again, a[(++ptr)->i].z)
=> a[(1005)->i].z [ since ptr=a(or a+0) =1000, then ++ptr=(a+1)=1005].
=> a[2].z [ since a[1].i=2 or *(a+1).i=2 ].
= Bangalore [since a[2].z=Bangalore ]
Again, a[--(ptr->p->i)].z
=>a[--(1005->p->i)].z [since currently ptr=(a+1)=1005).
=>a[ - -((a+2)->i)].z [since ptr->p = *(a+1)->p = a[1]=a+2 ].
=> a[--3].z [since (a+2).i =a[2].i = 3 ].
=> a[2].z [since - -3 =2].
=> Bangalore.
Therefore, ptr->z => Nagpur=> ++ptr->z = agpur.
Again, a[(++ptr)->i].z)
=> a[(1005)->i].z [ since ptr=a(or a+0) =1000, then ++ptr=(a+1)=1005].
=> a[2].z [ since a[1].i=2 or *(a+1).i=2 ].
= Bangalore [since a[2].z=Bangalore ]
Again, a[--(ptr->p->i)].z
=>a[--(1005->p->i)].z [since currently ptr=(a+1)=1005).
=>a[ - -((a+2)->i)].z [since ptr->p = *(a+1)->p = a[1]=a+2 ].
=> a[--3].z [since (a+2).i =a[2].i = 3 ].
=> a[2].z [since - -3 =2].
=> Bangalore.
Aayush Gajjar said:
2 years ago
Actually, if know the concept,
You'll not need to even calculate this program.
We know that any a[i].z will give the entire string.
And ++(ptr->z) or --(ptr->z) will not give the entire string.
So first will be not whole string,
Second will be whole string and;
third will be whole string.
That's why option (D) is correct because it is the only option which follows the above three conditions.
You'll not need to even calculate this program.
We know that any a[i].z will give the entire string.
And ++(ptr->z) or --(ptr->z) will not give the entire string.
So first will be not whole string,
Second will be whole string and;
third will be whole string.
That's why option (D) is correct because it is the only option which follows the above three conditions.
Shanmugam said:
1 decade ago
ptr holds the base address of a.ptr->z indicates the "Nagpur"i.e a[0].so ++(ptr->z) the pointer moves to the value a.so output is agpur.
next step is ++ptr moves the pointer to a[1]->i = 2.a[2].z = "Bangalore".
last step is now the prt at a[1].(a[1]->p) indicates a+2 i.e a[2]->i = 3,--3 = 2.a[2].z = "Bangalore".
next step is ++ptr moves the pointer to a[1]->i = 2.a[2].z = "Bangalore".
last step is now the prt at a[1].(a[1]->p) indicates a+2 i.e a[2]->i = 3,--3 = 2.a[2].z = "Bangalore".
Emanuel said:
9 years ago
How can it work for defining a struct by recursion?
Sravya said:
1 decade ago
I don't understand this please any one explain me.
Soumya shree said:
1 decade ago
@shanmugam:.
Please explain it in an easier way.
Please explain it in an easier way.
Keerthi said:
7 years ago
Can anyone please explain it in an easy manner?
Bhavya said:
1 decade ago
@shanmugam:.
Could you explain more clearly.
Could you explain more clearly.
Jagadeesh said:
10 years ago
Really superb but its too tough question.
M R Kamal said:
1 decade ago
I have not understand the last step.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers