241 views

What will be the output of the following program?

#include<stdio.h>
struct _myst{
char b[20];
char *a;
struct _myst *c;
}x[2] = {"GATE", "Overflow", x+1, "GO", "Classes", x}, *p = x;
typedef struct _myst myst;

myst* mystry(myst *p, int n){
if (n<=0) return p++;
if(n%2) return mystry(p->c, n-2);
else return mystry(p->c, n-1);
}
int main()
{
printf("%s",mystry(p,2023)->a);
}

1. GATE
2. Overflow
3. Run time error
4. Compile-time error

This one is interesting. Modulus 4 has cycle period of 4, i.e. it repeats the cycle after 4 iterations.

Number: 0 1 2 3 4 5 6 7 8 9...

Mod 4:    0 1 2 3 0 1 2 3 0 1…

String literals:

 “GATE” “GO” pointer to 1000 pointer to 2000 pointer to x+1 pointer to x

mystery(p, 2023), effectively mystery(x, 2023):

If(n <= 0): False

if(n%2) = if(1): True: return mystery(p → c, n – 2), effectively mystery(x + 1, 2021)

mystery(x + 1, 2021)

If(n <= 0): False

if(n%2) = if(1): True: return mystery(p → c, n - 2), effectively mystery(x, 2019)

Started with mystery(x, 2023) and at mystery(x, 2019): Cycle period of 2, with n – 2 in each.

Doing it exhaustively: 2023 mod 4 = 3. Finally we need to calculate mystery(x,3)

mystery(x, 3):

if(n <= 0): False

if(1): True: return mystery(p → c, n – 2) = mystery(x + 1, 1)

mystery(x + 1, 1):

if(n <= 0): False

if(1): True: return mystery(p → c, n – 2) = mystery(x, -1)

mystery(x, -1):

if(n <= 1): True: return p++; Here the p (which in x currently) will be returned first and then will be incremented (that also locally, though not effective in this question)

Inside main, return x, x → a = Overflow (pointer to string literal overflow)

@Shubhamishere So, you understand how mystery(pointer, value) , changes the pointer on each recursion step. Notice the following is happening with each call of the mystery function:

mystery(x, 2023)   ----   2023 mod 4 =3

mystery(x + 1, 2021) ----- 2021 mod 4 =1

mystery(x, 2019)  -----    2019 mod 4 = 3

mystery(x+1, 2017)  ----- 2017 mod 4 =1

Rest you can figure out on your own.

Why mod 4? See we want to know the first argument to the function mystery() , that is , the pointer

To know that we must understand the pattern. We can clearly see the pattern that, pointer returns back to address x when the second argument is 3mod 4 and pointer returns back to address x+1 when the second argument is 1mod 4.

I hope this clears your doubt.

Just use pen and paper and draw the structure and iterate, you’ll see it calls mystery with the reduced value of n every time. Do this 2-3 times and you’ll see a pattern.

edited by

We can also check like this :

For the pointer value to be x , the 2nd  Arg value = 2023 – 4k where k is a whole number

Similarly for the pointer value to be x+1 , the 2nd Arg value = 2021 – 4m where m is whole number.

Check for the least number greater than 0  for which the the pointer value becomes x or x+1

Here for m = 505 i.e (2nd Arg value =1) pointer value will become x+1.

mystry(p,2023) effectively becomes mystry(x+1 , 1)

The printf statement prints variable char *a of structure, So if it it prints the answer will be either overflow or classes. Show option A can not be answer.

By executing some program we can see it won’t be runtime error. So Opt C,D Not Possible.

This way of answering should not be practiced unless real exam. I had this kind of thought, so shared. Please correct me if my logic is wrong. Also inform me I will remove answer as tricks are not good for practice.
by