in Programming in C edited by
26,956 views
100 votes
100 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 in C edited by
27.0k 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
5
5

6 Answers

370 votes
370 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

36 Comments

such a beautiful "art work" truly appreciated :) you have drawn all "array & pointer" questions with the diagram i a very beautiful way.....will helpful for the beginner in the understanding  pointer concept
43
43
best answer
0
0

This is till date, the best answer I've come across on GO.

12
12
superb .......
1
1
best answer on GO so far amazing work
0
0
best explanation for practicing at home ...hope no one will try this whole step wise step  at exam hall
0
0
there is another question of Same type please do the same explanation again....
0
0
In 2004 paper
0
0
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 ???/
1
1
He has given brackets jst fr understanding purpose....
1
1
Wat a presentation ... GO should start Best answer award ... U will be the 1st winner i guess ... (y)
1
1
grand salute ..#grt no one can explain btr than this
4
4
its just wooooow
5
5
awesome explanation
4
4
edited by
@Debashish Deka

guys like you , makes this community such a great place for preparation.

Thankyou sir.
7
7
brilliance...!!
1
1
Salute sir excellent explanation
1
1

@Debashish Deka sir 

How to differentiate ++p -> c  &  p++ -> c

If I take precedence concept then first dereferencing will be done as for p -> c  & then the pointer p should increase for either p++ or ++p. But why for pre-increment line the character pointer is increasing instead of p??

3
3
have you seen the precedence rule I have mentioned? Out of the two cases, you presented, which expression need precedence resolution?
0
0
I think both the expression need precedence resolution.without precednce resolution how can I proceed ???   -> having higher precedence than ++
0
0

@Debashish Deka 

ok sir let me clear my doubt.

At the line of ++p -> c  first -> operator will be evaluated i.e p->c = 200. upto this it's okay but after that when p++ is executed then why p->c is increasing instead of p. I mean now p should be p = 1016(pointing to next structure).

This is happening exactly like in the next line p++ -> c , where after p->c , p is incrementing rather than (p -> c)++.

From this solution it's look like

++p -> c = p->c ; (p->c)++ ;

p++ -> c = p->c ; p++;

How is this happening???

0
0

$++p\rightarrow c$ means $p=p+1$ then $p\rightarrow c$

$p++\rightarrow c$ means $p\rightarrow c$ then $p=p+1$ 

$++(*p)$ means value at that position gets increased

means this

#include <stdio.h>
 
int main(void) {
	 char ptr[ ]="C programming";
    printf(" %c \n",++*ptr); 
	return 0;
}
 

o/p="D"

1
1

@srestha mam, can we evaluate lower precedence operator before higher precedence operator.as ++ having lower precedence than [ ].

If ++p -> c means p=p+ 1 then p->c. Then in that case p would be 1016 & p->c would be from j and in the next line p->c would print jungle.

Isn't it ????

 

​​

 

0
0
yes, I was wrong

$++p\rightarrow c=++(p\rightarrow c)$

As $\rightarrow$ has greater precedence than $++$ So, that will be evaluated first.

In this question , when $p$ is incrementing, it increments as size of pointer. but when $p\rightarrow c$ incrementing, it incrementing as size of a character, i.e. $1$ B
4
4

@srestha man, I can understand the pointer increment.

You please tell me one thing, if ++p->c is treated as ++(p->c) then p++ -> c  should be treated as (p->c)++

Isn't it?????

but here these both are treated as differently in this solution.

See ++p->c is ++(p->c) and p++ -> c is p->c then p++

This is my confusion.

3
3

@MRINMOY_HALDER ,

p++->c

 see the operand from the left, (it is p first), how many operators are attached with p? Only 1, and that is, ++.

Apply it to p and continue.

Now please see the difference between (operator) operand (operator) case, p++->c does not fall into this category. 

2
2

@Debashish Deka

Sir, I can't understand,could u please explain me a little bit more??

0
0

$p++\rightarrow c$ is equal to $(p++)\rightarrow c$

here we are not comparing precedence between $\rightarrow$ and $++$

Because $p$ is not linked with $\rightarrow$

We calculate precedence same variable attached with atleast 2 operator

but here that is not the case

13
13
Thanks srestha mam 😊
0
0

@MRINMOY_HALDER     p++->c is equal to ((p++)->c) //since p is post increment p->c will be evaluated first but p will be incremented later.

in case of ++p->c it will be equal to (++(p->c))// inner bracket will be evaluated first and then the outer bracker.

it all about precedence and associativity.

hope it helps

0
0
Yeah
2
2
The most amazing thing about this explanation is that he made students understood the concept without even speaking a word.Explaining what you think is very tough using just writing and visual pictures.
2
2
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 :) 🏆
2
2
such a clear and detailed explanation, hats off to the dedication!
0
0
66 votes
66 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