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

Pointer and Structure in C Part 1

Author Komal Shaw
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 1 Mark questions and solutions of Pointer and Structure in C from previous year's GATE papers. For the 2 marks questions, refer to Pointer and Structure in C Part-2.

1 Mark Questions

  1. What is printed by the following ANSI C program?

#include <stdio.h>

int main(int argc, char *argv[ ]) {

   int x = 1, z[2] = {10, 11};

   int *p = NULL;

   p = &x;

   *p = 10;

   p = &z[1];

   *(&z[0] + 1) += 3;

   printf("%d, %d, %d\n", x, z[0], z[1]);

   return 0;

}

(A) 1, 10, 11

(B) 1, 10, 14

(C) 10, 14, 11

(D) 10, 10, 14

 

ANS - (D)

Solution - 

The given C program is,

Line 0: int x=1, z[2] = {10, 11};

Line 1: int *p = NULL;

Line 2: p = &x;

Line 3: *p = 10;

Line 4: p = &z[1];

Line 5: *(&z[0] + 1) += 3;

Line 6: printf(“%d, %d, %d\n”, x, z[0], z[1]);

Line 5: *(&z[0] + 1) += 3;

=*(&z[0]+1) += 3

=*(3000+1)+=3

=*(3002)+=3

=11+=3

=14

X = 10

Z[0] = 10

Z[1] = 14

Hence the correct answer is 10, 10, 14.
 

2. Consider the following C program :

#include < stdio.h >

 int main () {

            int arr [] = {1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip = arr+4;

             printf ("%d\n", ip[1]);

             return 0;

}

The number that will be displayed on the execution of the program is _______.

(A) 6

(B) 5

(C) 4

(D) segmentation error

 

ANS - (A)

Solution - ip is an integer pointer, and the initial assignment sets it to the element at array index 4, i.e., 5. (holds the address of ar index 4)

The following statement refers to the next integer after it, which is 6(ip[1]=∗(ip+1)).
 

3. Consider the following C program.

#include< stdio.h >

struct Ournode{

 char x,y,z;

};

int main(){

 struct Ournode p = {'1', '0', 'a'+2};

 struct Ournode *q = &p;

 printf ("%c, %c", *((char*)q+1), *((char*)q+2));

 return 0;

}

The output of this program is:

(A) 0, c

(B) 0, a+2

(C) '0', 'a+2'

(D) '0', 'c'
 

ANS - (A)

Solution - since 'a' + 2 will be 'c', so Ournode p = {'1', '0', 'c'} and output will be 0, c.

 

4. Consider the following C program.

#include < stdio.h >

    void mystery(int *ptra, int *ptrb) {

    int *temp;

    temp = ptrb;

    ptrb = ptra;

    ptra = temp;

}

int main() {

    int a=2016, b=0, c=4, d=42;

    mystery(&a, &b);

    if (a < c)

       mystery(&c, &a);

    mystery(&a, &d);

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

}

The output of the program is _____________.
 

ANS - 2016

Solution - 

Since a and d are not swapped as the function mystery() doesn't change values but pointers that are local to the function.
 

5. Consider the following C program.

void f(int, short);

void main()

{

    int i = 100;

    short s = 12;

    short *p = &s;

    __________ ;    // call to f()

}

Which one of the following expressions, when placed in the blank above, will NOT result in a type-checking error?

(A) f(s, *s)

(B) i = f(i,s)

(C) f(i,*s)

(D) f(i,*p)

 

ANS - (D)

Solution - 

i is an integer, and *p is the value of a pointer to short.

1) Option 1 is wrong because we are passing '*S' as the second argument. Check that S is not a pointer variable. So the error occurs.

2) the second option is we are trying to store the value of f(i,s) into i but look at the function definition outside the main. It has no return type. It is simply void, so this assignment is wrong. So the error occurs.

3) Option 3 is wrong because of the same reason why option 1 is wrong

4) So option d is correct.

 

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

 

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

char c[] = "GATE2011";

// p now has the base address string "GATE2011."

char *p =c;

// p[3] is 'E' and p[1] is 'A'.

// p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4

// So the expression p + p[3] - p[1] becomes p + 4 which is

// base address of string "2011"

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

 

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

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

10. Consider the following C function:

void swap (int a, int b)

{

   int temp;

   temp = a;

   a = b;

   b = temp;

}

To exchange the values of two variables, x, and y.

(A) Call swap (x, y)

(B) Call swap (&x, &y)

(C) swap(x,y) cannot be used as it does not return any value

(D) swap(x,y) cannot be used as the parameters are passed by value
 

ANS - (D)

Solution - Because in the given function, parameters are used as passed by value. So there will be no change in actual parameters which are defined under the main function.
 

11. Assume the following C variable declaration

int * A[10], B[10][10];

Of the following expressions

I. A[2]

II. A[2] [3]

III. B[1]

IV. B[2] [3]
 

Which will not give compile-time errors if used as left-hand sides of assignment statements in a C program?

(A) I, II, and IV only

(B) II, III, and IV only

(C) II and IV only

(D) IV only
 

ANS - (A)

Solution - 

int main()

{

int *A[10], B[10][10];

int C[] = {12, 11, 13, 14};

/* No problem with below statement as A[2] is a pointer

and we are assigning a value to pointer */

A[2] = C;

/* No problem with below statement also as array style indexing

can be done with pointers*/

A[2][3] = 15;

/* Simple assignment to an element of a 2D array*/

B[2][3] = 15;

printf("%d %d", A[2][0], A[2][3]);

getchar();

}

 

12. Consider the following C declaration.

struct {

      short s[5];

      union {

      float y;

      long z;

      } u;

}t;

Assume that objects of the type short, float, and long occupy 2 bytes, 4 bytes, and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is

(A) 22 bytes

(B) 14 bytes

(C) 18 bytes

(D) 10 bytes

 

ANS - (C)

Solution -

Short array s[5] will take 10 bytes as the size of short is 2 bytes.

When we declare a union, the memory allocated for the union is equal to the memory needed for the largest member, and all members share this same memory space. Since u is a union, memory allocated to u will be the max of float y(4 bytes) and long z(8 bytes). So, the total size will be 18 bytes (10 + 8).

 

13. The most appropriate matching for the following pairs
 

X: m=malloc(5); m= NULL;

Y: free(n); n->value = 5;

Z: char *p; *p='a';

1: using dangling

2: using uninitialized pointers

3. lost memory

is:

(A) X—1 Y—3 Z —2

(B) (X—2 Y—1 Z –3

(C) X—3 Y—2 Z–1

(D) X—3 Y—1 Z–2

 

ANS - (D)

Solution - X:m=NULL

X:m=NULL; makes the pointer m point to NULL. But the memory created using malloc is still there but cannot be used as we don't have a link to it. Hence, lost memory

Y:n is freed and so pointer n is now pointing to an invalid memory making it a Dangling pointer.

Z:p is not initialized. p=malloc(sizeof(char)); should have been used before assigning 'a' to ∗p.
 

14. The following C declarations

struct node{

  int i:

  float j;

};

struct node *s[10];

define s to be

(A) An array, each element of which is a pointer to a structure of type node

(B) A structure of 2 fields, each field being a pointer to an array of 10 elements

(C) A structure of 3 fields: an integer, a float, and an array of 10 elements

(D) An array, each element of which is a structure of type node.

 

ANS - (A)

Solution - [] has greater precedence than ∗ in C. So, s becomes an array of pointers.
 

Click here to know about include stdio h here.

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

 

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