Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

GATE | GATE IT 2006 | Question 49

  • Last Updated : 28 Jun, 2021

Which one of the choices given below would be printed when the following program is executed ?

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

#include <stdio.h>
struct test {
               int i;
               char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
    struct test *p = st;
    p += 1;
    ++p -> c;
    printf("%s,", p++ -> c);
    printf("%c,", *++p -> c);
    printf("%d,", p[0].i);
    printf("%s \n", p -> c);

(A) jungle, n, 8, nclastor
(B) etter, u, 6, ungle
(C) cetter, k, 6, jungle
(D) etter, u, 8, ncestor

Answer: (B)


Basic Requirement – Knowledge of Structures

struct test *p = st;

st is a array of structures in which the integer is the “i” variable of structure and the character array is the “c” variable of structure.

p is a pointer of structure test type which is pointing to the array of structures (first element). p contains the base address of first structure in st array i.e { 5, become}.

p += 1;

This statement increments p by one which means p now point to the next structure (because p is pointer of type structure test, so on incrementing it by one it will jump one structure). p now points to {4, better}.

Before moving on to further statements we first discuss about the operators and their precedence.

“->” is structure operator which gets the member of the structure being referred. It has higher priority than increment/decrement (++, –) and dereferencing operation (*).

Now a brief discussion about post increment and pre increment operator.

Pre-increment operator is used to increment the value of variable before using in the expression. In the Pre-Increment value is first incremented and then used inside the expression.

Post-increment operator is used to increment the value of variable as soon as after executing expression completely in which post increment is used. In the Post-Increment value is first used in a expression and then incremented.

 ++p -> c; 

In this statement the structure operator (higher priority) will first make p point to the c variable inside the structure. Now pointer is inside the character array, now incrementing the pointer in character array ‘c’ will jump one character in character array. Hence the character array now starts from “e”.

p->c moves pointer inside the structure to the variable “c”.

printf("%s,", p++ -> c);

In this statement there is post increment operator used. First the statement will be executed and then operator will work. Currently p is pointing to second structure { 4, better} and inside structure the character array is starting from e because of the previous statement so p->c will print the string “etter” and after that the post increment operator will increment the structure and p now will point to next structure.

printf("%c,", *++p -> c);

In this statement p->c points to “jungle” (p was currently pointing to 3rd structure {6, jungle}) and then the pre incrementing operator just like in previous pre increment statement moves one character and now the pointer points to “u”, dereferencing operator here prints the value at the address where the pointer is pointing and since the pointer is pointing at “u” so u is printed.

printf("%d,", p[0].i);

In this statement p[0] will give the address where p is currently pointing i.e {6, jungle} and the structure operator (.) will point to the “i” variable of the structure printing 6 as a result.

printf("%s \n", p -> c);

This statements prints the character string of the structure to which p is currently pointing and since because of earlier increment of character array, character array starts from “u” therefore “ungle” is printed.

This explanation has been contributed by Parul Sharma.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!