The Gateway to Computer Science Excellence
+46 votes
6.5k views

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 by Boss (16.3k points)
edited by | 6.5k views
0
Legendary explanation ...thanks a lot
0

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

because of   

struct test {
int i;
char *c;
}

 

 

7 Answers

+213 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$

by Veteran (57k points)
edited by
+30
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
0
best answer
+8

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

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

@Debashish Deka

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

Thankyou sir.

0
brilliance...!!
0
Salute sir excellent explanation
+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??

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

$++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"

0

@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 ????

 

​​

 

+1
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
+1

@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.

+1

@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. 

0

@Debashish Deka

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

+4

$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

0
Thanks srestha mam 😊
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

+48 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

by Junior (985 points)
0
p is a pointer to structure. So, why ++(p ->c) increments p by $1$B. Why it doesn't increment the value of character.
0
It is incrementing the position of character string..
+2

This answer shows moving p, but p does not move to the middle of a struct member. The member char* is incremented. movement of p will be always from st[x] to st[x+1] when we perform p++

+2

Puja Mishra 

It is incrementing the position of a character string. NO

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

0
great explanation.......
0

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

+18 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

by Boss (41.6k points)
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
-> is not given priority in post inc, () is there to show ++ is associated with p not with p->c
+14 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.

by Loyal (9.5k points)
+4
@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.
+1
++p points to one letter of word 'e' so how can p++ point directly to next element of structure?
0
p is a pointer to structure. So, why ++(p ->c) increments p by 11B. Why it doesn't increment the value of character.
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"}]

+4 votes

Ans is option b

by Loyal (7k points)
–1
@gateasp17

can u tell why there is different memory locations for s[0] s[1] ?
0
First of all there is no array name s[ ].

its st[ ]  I guess.. Here I assume THAT to store an address 8Byte is required and int requires 2 byte..  which makes st[0] location 6000 then st[1] location 6010 .
0
it was typo it is st right not s

Here I assume THAT to store an address 8Byte?what does it signify

whom address u r going to store?

i compile it on my machine it give successive difference of 8  in memory address
0
I mentioned in picture assume pointer size is 8 byte as in struct one int and one char pointer so take 2Byte for int and 8 byte for char. And these sizes (int char, float) can vary machine to machine as I know.

What do you want know?  

If u get succesive difference of 8 byte, Can u tell Me how much size is require in ur machine ro store an integer and character pointer or any pointer separately??
+3 votes
answer is :

option B

etter,u,6,ungle
by Active (1.9k points)
0
pretty simple logic so i am not mentioning the flow... anyone wants it then i may edit my answer.
0
Gott it
0
Please explain ur solution.
+3 votes
Answer: B
by Boss (33.8k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,557 answers
195,717 comments
101,584 users