in Programming edited by
15,516 views
91 votes
91 votes

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

#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);
}
  1. $\text{jungle, n, 8, nclastor}$
  2. $\text{etter, u, 6, ungle}$
  3. $\text{cetter, k, 6, jungle}$
  4. $\text{etter, u, 8, ncestor}$
in Programming edited by
15.5k views

3 Comments

Legendary explanation ...thanks a lot
0
0

many people are confusing why increment by 1000 to 1008 why not 1004 ? 

because of   

struct test {
int i;
char *c;
}

 

 

0
0
4
4

6 Answers

350 votes
350 votes
Best answer

code :

#include <stdio.h>

struct test {
    int i;
    char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};

int main () { 
    //printf("size = %d\n",sizeof(struct test) );
    struct test *p = st;
    p += 1;
    ++p->c; // ++(p->c)
    printf("%s,", p++->c); // (p++)->c 
    printf("%c,", *++p->c); // *(++(p->c))
    printf("%d,", p[0].i);
    printf("%s \n", p->c);
}

We will assume few things:

  • Size of integer $4$ Bytes.
  • Size of a pointer $4$ Bytes.

Neglecting any alignment issues with the storage of this structure we will have $8$ Bytes per structure. 

And one precedence rule we need to use:


Initial situation :


struct test *p = st;


p += 1;

We know that if ptr is a pointer then,  ptr + x = ptr + x*sizeof(*ptr);


++p->c;


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


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


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


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

Correct Answer: $B$

edited by
by

4 Comments

Everything is perfect But I think,

P[0].i is same as (*(P+0)).i and  it's same as P->i here
0
0
Please someone clear my doubt here,

in the line ++p-> c;  just before print, here it will evaluated as ++(p->c) and P will point to first e of better, after evaluating this, Is this new address being store in P, and the value of P been changed to first e of better ? Or P still point to p[1]?
0
0
Awarded for the best answer :) 🏆
1
1
65 votes
65 votes
#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);
    

Prints value and  increments the pointer. Pointing ti the next structure

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

printf("%d,", p[0].i); //Prints the integer value associated with struct pointed by p

printf("%s \n", p -> c); //Prints the character string pointed by p

PS : Thank you @monanshi for the explanation

4 Comments

Puja Mishra 

It is incrementing the position of a character string. NO

++p->c does not increment p at all.

3
3
great explanation.......
0
0

thor It is incrementing to the next structure in the array as p is the pointer to struct test

0
0
23 votes
23 votes

Answer is B )

Run The code.

http://codepad.org/WmSREd4G

For extra inofrmation =>

++p -> c will become ++(p -> c)
p++ -> c will become (p++) -> c
 *++p -> c will become => *++(p->c)

Reference-> C Puzzle Book

2 Comments

in case of pre increment on p , ---> operator is given more priority  by applying brackets

in case of post increment on p , ----.> operator is given less priority  by applying brackets on p++       

 

please explain why ???/
0
0
-> is not given priority in post inc, () is there to show ++ is associated with p not with p->c
2
2
15 votes
15 votes

I am getting B.

 

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


Line 8 => Initially p is pointing to st, i.e, first element of st which is {5, "become"}
Line 9 => Now p is pointing to {4, "better"}
Line 10=> ++(p->c) since -> has higher precedence. So, p->c points to 'e' of 'better'
Line 11=> prints 'etter' and p now points to {6, "jungle"}
Line 12=> *++(p->c) since -> has higher precedence. prints 'u'
Line 13=> p->i, which is 6 so prints '6'
Line 14=> prints 'ungle' since p is pointing to 'u'.

So, output is "etter, u, 6, ungle" that is B.

4 Comments

@monanshi your explanation has errors. p is a pointer to structure test and it can only point to structures of the same type, so p will never point to 'e', rather p->c points to 'e' after ++p->c, same thing for line 14, when p->c points to u, not p.
5
5
++p points to one letter of word 'e' so how can p++ point directly to next element of structure?
1
1
p is a pointer to structure. So, why ++(p ->c) increments p by 11B. Why it doesn't increment the value of character.
0
0

Nice Explanation Monashi. But I have added few steps

Initially st => [{5, c0},{4,c1},{6,c2},{8,c3},{7,c4}]

Suppose address of variable c is where ci are:

c0 => "become"
c1 => "better"
c2 => "jungle"
c3 => "ancestor"
c4 => "brother"


Line 8 => Initially p is pointing to st, i.e, first element of st which is {5, c0} 
Line 9 => Now p is pointing to {4, c1} 
Line 10=> ++(p->c) since -> has higher precedence. [ Means: p->c= (p->c)+1 Therefore address of c1 is incremented by 1 and thus c1 => "etter"] 
Line 11=> prints 'etter' [because c1 => "etter" now ] and p now points to {6, "jungle"} 
Line 12=> *++(p->c) since -> has higher precedence. [ Means: ++(p->c) => (p->c= (p->c)+1) Therefore address of c2 is incremented by 1 and thus c2 => "ungle"  and * (++(p->c)) prints the character pointing to, i.e 'u' ] 
Line 13=> p->i, which is 6 so prints '6' 
Line 14=> prints 'ungle' [Because of modification in the string c2 at Line 12]

Post Execution st => [{5, "become"},{4,"etter"},{6,"ungle"},{8,"ancestor"},{7,"brother"}]

0
0
Answer:

Related questions