Table of contents
1.
Introduction
2.
2 Marks Questions
3.
5 Marks Question
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pointer and Structure in C Part-2

Author Komal Shaw
0 upvote

Introduction

If you are an undergraduate or graduate student interested in pursuing a postgraduate degree in science or technology, you should be aware of the GATE exam. GATE is one of the most prestigious exams for engineering students. Many students aspire to score well in this exam to kick start their careers. In this blog, we will go through the 2 Marks questions and solutions of Pointer and Structure in C from previous year's GATE papers. Refer to Pointer and Structure in C Part 1 for 1 mark questions.

2 Marks Questions

  1. Consider the following C program:

      #include < stdio.h >

       int main()

       {

           int a[] = {2, 4, 6, 8, 10} ;

           int i, sum = 0, *b = a + 4 ;

           for (i = 0; i < 5; i++)

                 sum = sum + (*b - i) - *(b - i) ;

           printf ("%d\n", sum) ;

           return 0 ;

        }

The output of the above C program is _____.
 

ANS - 10

Solution - 

i = 0

sum = 0+ 10 – 10 = 0

i = 1

sum = 0 + 9 – 8 = 1

i = 2

sum = 1 + 8 – 6 = 3

i = 3

sum = 3 + 7 – 4 = 6

i = 4

sum = 6 + 6 – 2 = 10

 

2. Consider the following C program:

#include< stdio.h >

void fun1(char *s1, char *s2){

  char *tmp;

  tmp = s1;

  s1 = s2;

  s2 = tmp;

}

void fun2(char **s1, char **s2){

  char *tmp;

  tmp = *s1;

  *s1 = *s2;

  *s2 = tmp;

}

int main(){

  char *str1 = "Hi", *str2 = "Bye";

  fun1(str1, str2); printf("%s %s ", str1, str2);

  fun2(&str1, &str2); printf("%s %s", str1, str2);

  return 0;

}

The output of the program above is

(A) Hi Bye Bye Hi

(B) Hi Bye Hi Bye

(C) Bye Hi Hi Bye

(D) Bye Hi Bye Hi
 

ANS - (A)

Solution -

fun1(char *s1, char *s2)

The above function scope is local, so the value changed here won't affect actual parameters. SO the values will be 'Hi Bye.' 

fun2(char **s1, char **s2)

In this function, value is pointer to pointer, so it changes the pointer of the actual value. So values will be 'Bye Hi.'
 

3. What is the output of the following C code? Assume that the address of x is 2000 (in decimal), and an integer requires four bytes of memory.

int main () { 

unsigned int x[4][3] = 

          {{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; 

          printf(“%u, %u, %u”, x+3, *(x+3), *(x+2)+3); 

}

(A) 2036, 2036, 2036

(B) 2012, 4, 2204

(C) 2036, 10, 10

(D) 2012, 4, 6

 

ANS - (A)

Solution - 

x = 2000

Since x is considered as a pointer to an 

array of 3 integers and an integer takes 4

bytes, value of x + 3 = 2000 + 3*3*4 = 2036

The expression, *(x + 3) also prints same 

address as x is 2D array.

The expression *(x + 2) + 3 = 2000 + 2*3*4 + 3*4

                            = 2036
 

4. Consider the following C code segment.

int a, b, c = 0;

void prtFun(void);

main( )

   static int a = 1; /* Line 1 */

   prtFun();

   a + = 1;

   prtFun();

   printf("\n %d %d ", a, b);

}

void prtFun(void)

   static int a=2; /* Line 2 */

   int b=1;

   a+ = ++b;

   printf("\n %d %d ", a, b);

}

What output will be generated by the given code segment if:

Line 1 is replaced by auto int a = 1;

Line 2 is replaced by register int a = 2;

(A)

3 1

4 1

4 2

(B)

4 2

6 1

6 1

(C)

4 2

6 2

2 0

(D)

3 1

5 2

5 2

 

ANS - (C)

Solution -

main

a=1

prtFun()

a=2

b=1

a=a+++b=2+2=4

b=2

printf →4 2

back to main

a=a+1→1+1→2 (local static a is taken)

prtFun()

a=4 // previous value in the function is retained because of static

b=1

a=a+++b=4+2=6

b=2

printf →6 2

back to main

a=2

b=0 (initial value of global b. in prtFun local b is only updated)

printf →2 0

 

5. The output of the following C program is__________.

void f1(int a, int b) { 

int c; 

c=a; a=b; b=c; 

}

void f2(int *a, int *b) { 

int c; 

c=*a; *a=*b; *b=c; 

}

int main(){ 

int a=4, b=5, c=6; 

f1(a,b); 

f2(&b, &c); 

printf(“%d”,c-a-b); 

}

(A) -5

(B) -4

(C) 5

(D) 3

ANS - (A)

Solution - The function call to f1(a, b) won't affect the values that are passed by value.

The function call f2(&b, &c) swaps values of b and c. So b becomes 6 and c becomes 5. The value of c-a-b becomes 5-4-6, which is -5.
 

6. What does the following fragment of the C-program print?

char c[ ] = "GATE2011";

char *p = c;

printf("%s", p + p[3] - p[1]);

(A) GATE2011

(B) E2011

(C) 2011

(D) 011

 

ANS - (C)
Solution - 

2011 is the answer. 

In C, there is a rule that whatever character code be used by the compiler, codes of all alphabets and digits must be in order. So, if character code of 'A' is x, then for 'B' it must be x+1. 

Now %s means printf takes and address and prints all bytes starting from that address as characters till any byte becomes the code for ′\0′. Now, the passed value to printf here is p+p[3]−p[1]

p is the starting address of array c.p[3] = ′E′ p[1]=′A′. So, p[3]−p[1]=4, and p+4 will be pointing to the fifth position in the array c. So, printf starts printing from 2 and prints 2011. 

(Here ‘‘GATE2011” is a string literal and by default a ′\0′ is added at the end of it by the compiler). 

NB: In this question %s is not required. 

printf(p + p[3] - p[1]); 
Also gives the same result as the first argument to printf is a character pointer and only if we want to pass more arguments do we need to use a format string. 
 

7. What does the following program print?

#include < stdio.h >

void f (int *p, int *q) {

     p = q;

    *p = 2;

}

int i = 0, j = 1;

int main ( ){

     f(&i, &j);

     printf ("%d %d \ n", i, j);

     return 0;

}

(A) 2 2

(B) 2 1

(C) 0 1

(D) 0 2
 

ANS - (D)

Solution -

In p the address of i will be stored first and in q the address of j will be stored. Then in the second turn, the address of j will be stored in p. Thus, in the end, 0 and 2 will be printed.
 

8. What does the following C statement declare?

           int (*f)(int *);

(A) A function takes an integer pointer as an argument and returns an integer.

(B) A function takes an integer as an argument and returns an integer pointer.

(C) A pointer to a function that takes an integer pointer as an argument and returns an integer.

(D) A function that takes an integer pointer as an argument and returns a function pointer.
 

ANS - (C)

Solution - 

The steps to read complicated declarations :

  1. Convert the C declaration to postfix format and read from left to right.
     
  2. To convert expressions to postfix, we need to start from the innermost parentheses. If the innermost parenthesis is not present, start from the declaration's name and go right first. When first ending parenthesis encounters, then go left. Once the whole parenthesis is parsed, then come out from parenthesis.
     
  3. Continue until the complete declaration has been parsed.

At First, we convert the following given declaration into postfix:

int ( * f) (int * )

Since there is no innermost bracket, we first take declaration name f, so print "f" and then go to the right. Since there is nothing to parse, so go to the left. There is * on the left side, so print "*."Come out of parenthesis. Hence postfix notation of a given declaration can be written as follows:

f * (int * ) int

Meaning: f is a pointer to function (which takes one argument of int pointer type) returning int.

 

9. Consider the C program shown below.

#include < stdio.h >

#define print(x) printf("%d ", x)

int x;

void Q(int z) {

   z += x; print(z);

}

void P(int *y) {

   int x = *y+2;

   Q(x); *y = x-1;

   print(x);

}

main(void) {

   x = 5;

   P(&x);

   print(x);

}

The output of this program is

(A) 12 7 6

(B) 22 12 11

(C) 14 6 6

(D) 7 6 6
 

ANS - (A)

Solution - x is global, so the first x becomes 5 by the first line in the main(). Then main() calls P() with the address of x.

// in main(void)

x = 5 // Change global x to 5

P(&x)

P() has a local variable named 'x' that hides the global variable. P() then calls Q() by passing value of local 'x'.

// In P(int *y)

int x = *y + 2; // Local x = 7

Q(x);

In Q(int z), z uses x which is global

// In Q(int z)

z += x; // z becomes 5 + 7 

printz(); // prints 12

After the end of Q(), control comes back to P(). In P(), *y (y is address of global x) is changed to x – 1 (x is local to P()).

// Back in P()

 *y = x - 1; // *y = 7-1

 print(x); // Prints 7

After end of Q(), control comes back to main(). In main(), global x is printed.

// Back in main()

print(x); // prints 6 (updated in P()

          // by *y = x - 1 )

 

10. Consider the following three C functions:

[P1] int*g(void)

     {

       int x=10;

       return(&x);

     }

[P2] int*g(void)

      {

        int *px;

       *px = 10;

        return px;

      }

[P3] int*g(void)

     {

       int *px

       px =(int*)malloc (size of (int));

       *px = 10;

       return px;

     }

Which of the above three functions are likely to cause problems with pointers?

(A) Only P3

(B) Only P1 and P3

(C) Only P1 and P2

(D) P1, P2, and P3
 

ANS - (C)

Solution -

[P1] may cause an error because function is returning the address of locally declared variable.

[P2] will cause a problem because px is an int pointer that is not assigned with any address and we are doing dereferencing.

[P3] will work because memory in bytes of size of int will be reserved and its address will be stored in px that can be further use, once function execution completes, this m/m will still exist in Heap until we free it using free() function.

5 Marks Question

  1. Consider the following C program:

void abc(char *s)

{

   if(s[0]=='\0') return;

   abc(s+1);

   abc(s+1);

   printf("%c",s[0]);

}

main()

   abc("123");

}

(a) What will be the output of the program?

(b) If abc(s) is called with a null-terminated string s of length n characters (not counting the null ('\0') character), how many characters will be printed by abc(s)?
 

ANS -

(a) 332 332 1

(b) (2n - 1)

Solution -

Ans for A 

O/p : 3323321

ans for B is: 2^n -1

for length 2 -> 3 characters

for length 3-> 7 characters

for length 4-> 15 characters

hence for n-> 2^n - 1 characters


Must Read Decision Making in C

Frequently Asked Questions

  1. What is the eligibility criteria for the GATE Exam?
    The candidate should have done their Bachelors in Science or Engineering.

     
  2. What is GATE?
    GATE, or Graduate Aptitude Test in Engineering, is an All-India Examination conducted jointly by the Indian Institute of Science (IISc), Bangalore, and the seven Indian Institutes of Technology (IITs).

     
  3. What are the questions asked in GATE?
    Two types of MCQs: MCQs - 1 mark for each correct answer; 1/3 mark will be deducted for every wrong answer. MCQs - 2 marks for each correct answer; 2/3 marks for every incorrect response will be deducted. There are no negative markings for Numerical Answer Type (NAT) questions.

     
  4. Are there any restrictions or age limits to applying for the GATE Exams 2023?
    No, there are no restrictions on the number of times. 

     
  5. Is a 3rd-year B.A./B.Com./B.Sc. student eligible to appear for GATE 2021?
    Yes, any undergraduate student currently in the 3rd year or higher years of any government-approved program in Engineering / Technology / Architecture / Arts / Commerce / Science is eligible to appear for GATE 2021. 

Conclusion

In this article, we have extensively discussed the approach and solutions to the past year's questions of Pointer and Structure in C asked in GATE. We hope this blog has helped you enhance your knowledge of the concepts and process of the questions.

If you want to learn more, check out our articles on Introduction to GATEHow to prepare in the Last 10 days to score high in GATE?Basic of Programming Language Part-1Basic of Programming Language Part-2, Pointer and Structure in C Part 1

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enrol in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

 

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass