search
Log In
60 votes
8.3k 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
edited by
8.3k 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;
}

 

 

0

9 Answers

255 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
2
grand salute ..#grt no one can explain btr than this
2
its just wooooow
1
awesome explanation
2

@Debashish Deka

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

Thankyou sir.

1
brilliance...!!
1
Salute sir excellent explanation
2

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

 

​​

 

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

6

$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

53 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

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

19 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

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.

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

–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
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
0 votes
Lets take the part of program,
Line 1 - main ( )
Line 2 - {
Line 3 - struct test *p = st;
Line 4 - p += 1;
Line 5 - ++p β†’ c;
Line 6 - printf("%s", p++β†’ c);
Line 7 - printf("%c", +++p β†’ c);
Line 8 - printf("%d", p[0].i);
Line 9 - printf("%s\n", p β†’ c);
Line 10 - }
Now,
Line 3: Initially p is pointing to st, i.e., first element of st which is {5, "become"}
Line 4: Now p is pointing to {4, "better"}
Line 5: ++(p β†’ c), since β†’ has higher precedence, so p β†’ c points to 'e' of "better".
Line 6: prints 'enter' and p now points to {6, "jungle"}
Line 7: ***(p β†’ c), since β†’ has higher precedence. So, prints 'u'.
Line 8: p β†’ i, which is 6 so prints '6'.
Line 9: prints 'ungle' since p is pointing to 'u'.
So, output is "enter, u, 6, ungle".
Answer:

Related questions

47 votes
7 answers
1
4k views
Which one of the choices given below would be printed when the following program is executed? #include <stdio.h> int a1[] = {6, 7, 8, 18, 34, 67}; int a2[] = {23, 56, 28, 29}; int a3[] = {-12, 27, -31}; int *x[] = {a1, a2, a3}; void print(int *a[]) { printf("%d,", a[0][2]); printf("%d,", ... } main() { print(x); } $8, -12, 7, 23, 8$ $8, 8, 7, 23, 7$ $-12, -12, 27, -31, 23$ $-12, -12, 27, -31, 56$
asked Nov 1, 2014 in Programming Ishrat Jahan 4k views
52 votes
6 answers
2
7.6k views
Which one of the choices given below would be printed when the following program is executed? #include <stdio.h> void swap (int *x, int *y) { static int *temp; temp = x; x = y; y = temp; } void printab () { static int i, a = -3, b = -6; i = 0; while (i <= 4) { if ((i++)%2 == 1) continue; a = a + i; b = ... $a = 12, b = 9$ $a = 3, b = 6$ $a = 3, b = 6$ $a = 6, b = 3$ $a = 15, b = 12$
asked Nov 1, 2014 in Programming Ishrat Jahan 7.6k views
4 votes
2 answers
3
728 views
Match the following concepts and their best possible descriptions. Concept Description i. overloading a. allows to define a class to have properties of another class ii. friend b. defining a set of similar functions iii. constructor c. used in dereferencing iv. protected d. used to give a non-member function access to the ... i-c, ii-f, iii-h, iv-a, v-g, vi-d i-b, ii-e, iii-c, iv-f, v-g, vi-s
asked Nov 1, 2014 in Object Oriented Programming Ishrat Jahan 728 views
18 votes
4 answers
4
2.4k views
What is the output printed by the following C code? # include <stdio.h> int main () { char a [6] = "world"; int i, j; for (i = 0, j = 5; i < j; a [i++] = a [j--]); printf ("%s\n", a); } dlrow Null string dlrld worow
asked Oct 29, 2014 in Programming Ishrat Jahan 2.4k views
...