Last Updated :
17 Dec, 2018
What will be the output of the C program given below.
#include <iostream>
#include <string>
struct s {
int i;
char *c;
}
str[] = {100, \"eclasses\", 200, \"geeks\",300, \"data\",400,\"structure\",500,\"students\"};
main ()
{
struct s *p = str;
p += 1;
printf(\"%s\", p -> c);
p--;
printf(\"%s,\", ++p -> c);
printf(\" %d, \", p[1].i);
p=p+4;
printf(\"%s\", p -> c);
}
(A) geekseclasses, 200, students
(B) geekseclasses, 100, students
(C) geeksclasses, 200, students
(D) None of the above
Answer: (C)
Explanation: Here structure is the data type contains integer type and string. Base address of strings,
eclasses=1000
geeks=2000
data=3000
structure=4000
students=5000
p contains the base address of the str data type.
p=p+1
means jump to index 0 to index 1 <200,2000>in array str.
First print statement, p->c points to the base address of the string and print statement print till null found in the string.
geeks
Second print statement
p– means back to index 0,
++p -> c means traverse the string one byte and print the remaining string
classes,
Third print statement
p[1].i prints the integer stored at 1 index.
200,
Fourth print statement
p=p+4 means go to index 4
p->c print string stored at index 4
students
So, option (C) is correct.
Quiz of this Question
Share your thoughts in the comments
Please Login to comment...