Consider the following C program :
#include <stdio.h> #include <string.h> struct s {
int i;
char *c;
} str[] = {100, "eclasses" , 200, "geeks" ,300, "data" ,400, "structure" ,500, "students" };
int 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);
} |
Which of the string(s) will be in the final output of the above C program ?
Note – This question is multiple select questions (MSQ).
(A) geeksclasses
(B) 100
(C) students
(D) 200
Answer: (A) (C) (D)
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 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
Therefore, options (A), (C), and (D) are correct.
Quiz of this Question
Please comment below if you find anything wrong in the above post