chapter
    Pointers, Arrays, Strings and Memory Management PYQs for GATE CS

    Solve 10+ Pointers, Arrays, Strings and Memory Management previous year questions for GATE CS with answers and detailed solutions. Free sample questions below

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    2026 Slot Set2 PYQ
    Level 3: Exam Standard

    In C runtime environment, which one of the following is stored in heap?

    Question 2
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following ANSI-C program.

    #include <stdio.h>
    
    int main(){
    
                  int *ptr, a, b, c;
                  a=5; b=11; c=20;
                  ptr=&a; *ptr=c; ptr=&c;
                  a=*(&b); c=*ptr-a;
                  printf("%d",c);
                  return(0);
    }

    The output of this program is ____________. (answer in integer)

    Note: Assume that the program compiles and runs successfully.
    Question 3
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following C program:

    #include <stdio.h>
    
    int main(){
    
            int a;
    
            int arr[5] = {30,50,10};
    
            int *ptr;
    
            ptr = &arr[0] + 1;
    
            a = *ptr;
    
            (*ptr)++;
    
            ptr++;
    
            printf("%d", a + (*ptr) + arr[1]);
    
            return 0;
    
    }

    The output of the above program is ___________. (Answer in integer)
    Question 4
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following C program:

    #include <stdio.h>
    
    void stringcopy(char *, char *);
    
    int main(){
    
            char a[30] = "@#Hello World!";
    
            stringcopy(a, a + 2);
    
            printf("%s\n", a);
    
            return 0;
    
    }
    
    void stringcopy(char *s, char *t) {
    
            while(*t)
    
                   *s++ = *t++;
    
    }

    Which ONE of the following will be the output of the program?
    Question 5
    2025 Slot Set1 PYQ
    Level 3: Exam Standard
    #include <stdio.h>
    void foo(int *p, int x){
    *p=x;
    }
    int main(){
    int *z;
    int a = 20, b = 25;
    z = &a;
    foo(z,b);
    printf("%d",a);
    return 0;
    }

    The output of the given C program is __________. (Answer in integer)
    Question 6
    2024 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following C function definition.

    int fX(char *a){
     char *b = a;
     while(*b)
      b++;
     return b - a;}

    Which of the following statements is/are TRUE?
    Question 7
    2024 Slot Set2 PYQ
    Level 3: Exam Standard
    What is the output of the following C program?

    #include <stdio.h>

    int main() {
      double a[2]={20.0, 25.0}, *p, *q;
      p = a;
      q = p + 1;
      printf(”%d,%d”, (int)(q – p), (int)(*q – *p));
      return 0;}
    Question 8
    2022 PYQ
    Level 3: Exam Standard
    What is printed by the following ANSI C program?

    #include<stdio.h>
    int main(int argc, char *argv[])
    {
           int a[3][3][3] =
           {{1, 2, 3, 4, 5, 6, 7, 8, 9},
            {10, 11, 12, 13, 14, 15, 16, 17, 18},
            {19, 20, 21, 22, 23, 24, 25, 26, 27}};
           int i = 0, j = 0, k = 0;
           for( i = 0; i < 3; i++ ){
                  for(k = 0; k < 3; k++ )
                         printf("%d ", a[i][j][k]);
                  printf("\n");
           }
           return 0;
    }
    Question 9
    2022 PYQ
    Level 3: Exam Standard
    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;

    }
    Question 10
    2021 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following ANSI C program.

    #include <stdio.h>
    int main(){
        int arr[4][5];
        int i, j;
        for (i=0; i<4; i++){
            for (j=0; j<5; j++){
                arr[i][j] = 10*i + j;
            }
        }
        printf("%d", *(arr[1] + 9));
        return 0;
    }

    What is the output of the above program?
    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Pointers, Arrays, Strings and Memory Management PYQs for GATE CS

    Solve 10+ Pointers, Arrays, Strings and Memory Management previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Pointers, Arrays, Strings and Memory Management

    Chapter Roadmap: Pointers, Arrays, Strings and Memory Management

    Mastering memory is the key to mastering C. This chapter builds your understanding from basic pointer mechanics to advanced dynamic memory allocation.

    1
    Pointer Assignment, Dereferencing and Arithmetic
    Current Topic. The address-of and dereference operators, pointer math, and tracing variable modifications. (High Weightage)
    2
    Strings and Character Pointers
    String literals, array of characters vs. pointer to char, and standard string manipulation logic.
    3
    Multidimensional Arrays and Memory Layout
    Row-major order, pointer to arrays, and calculating memory addresses in 2D and 3D arrays.
    4
    Runtime Memory and Dynamic Allocation
    Stack vs. Heap, malloc, calloc, realloc, free, and memory leak prevention.

    By the end of this chapter, you will:

    • Confidently trace complex pointer and array manipulations.
    • Understand exactly how C maps data structures to physical memory addresses.
    • Avoid common pitfalls like dangling pointers, memory leaks, and undefined behavior.

    Pointer Assignment, Dereferencing and Arithmetic

    Pointer Assignment, Dereferencing and Arithmetic

    Pointers are variables that store memory addresses. Understanding how to assign, dereference, and perform arithmetic on them is the absolute foundation of C programming.

    Why this matters:

    Pointers are the bridge between your code and the computer's physical memory. Mastering assignment, dereferencing, and arithmetic is the absolute prerequisite for every advanced C concept, from arrays to dynamic memory allocation.

    What you will learn here:

    • How to obtain and store memory addresses using the address-of operator.
    • How to read and modify values using the dereference operator.
    • The rules of pointer arithmetic and how it scales with data types.
    • How to systematically trace pointer manipulations in code.

    Pointers, Arrays, Strings and Memory Management: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming and Data Structures · 2026_Set2 MCQ

    In C runtime environment, which one of the following is stored in heap?

    1. A.

      A static variable declared inside a function

    2. B.

      An array of integers declared inside a function

    3. C.

      A dynamically allocated array of integers created using malloc() function call

    4. D.

      Return address of a function

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: This is a memory layout classification question, recognizable by asking where specific C constructs reside in the runtime environment.

    Exam route: Eliminate options based on standard C memory segments. Static variables go to Data/BSS. Local arrays and return addresses go to Stack. Only malloc targets the Heap.

    Learning route:

    1. Static variable inside a function: Stored in the Data segment (or BSS if uninitialized), not the Heap.
    2. Array of integers declared inside a function: This is a local variable, allocated on the Stack.
    3. Dynamically allocated array using malloc(): Explicitly requests memory from the Heap at runtime.
    4. Return address of a function: Pushed onto the Stack as part of the function call frame.

    Therefore, only the dynamically allocated array resides in the Heap.

    Question 2 · Programming and Data Structures · 2026_Set2 NAT
    Consider the following ANSI-C program.

    #include <stdio.h>
    
    int main(){
    
                  int *ptr, a, b, c;
                  a=5; b=11; c=20;
                  ptr=&a; *ptr=c; ptr=&c;
                  a=*(&b); c=*ptr-a;
                  printf("%d",c);
                  return(0);
    }

    The output of this program is ____________. (answer in integer)

    Note: Assume that the program compiles and runs successfully.
    Correct Answer:

    9.00

    Step-by-Step Solution

    Insight: This is a pointer tracing question, recognizable by sequential pointer assignments and dereferences that modify variable states in place.

    Exam route: Track the values of a, b, c, and ptr line by line. a becomes 20, then is overwritten to 11. c remains 20 until the final subtraction 20 - 11 = 9.

    Learning route:

    1. Initial state: a = 5, b = 11, c = 20.
    2. ptr = &a: ptr now holds the address of a.
    3. *ptr = c: Dereferencing ptr accesses a, so a is updated to the value of c, which is 20.
    4. ptr = &c: ptr is reassigned to hold the address of c.
    5. a = *(&b): The address-of and dereference operators cancel out, so a is assigned the value of b, which is 11 (overwriting the previous 20).
    6. c = ptr - a: ptr evaluates to the current value of c (which is 20). Thus, c = 20 - 11 = 9.
    7. The program prints 9.
    Question 3 · Programming and Data Structures · 2025_Set2 NAT
    Consider the following C program:

    #include <stdio.h>
    
    int main(){
    
            int a;
    
            int arr[5] = {30,50,10};
    
            int *ptr;
    
            ptr = &arr[0] + 1;
    
            a = *ptr;
    
            (*ptr)++;
    
            ptr++;
    
            printf("%d", a + (*ptr) + arr[1]);
    
            return 0;
    
    }

    The output of the above program is ___________. (Answer in integer)
    Correct Answer:

    111.00

    Step-by-Step Solution

    Insight: This is a pointer tracing question with mixed updates, recognizable by sequential pointer assignments, dereferences, and increments on an array.

    Exam route: ptr points to arr[1] (50). a becomes 50. (*ptr)++ makes arr[1] 51. ptr++ moves ptr to arr[2] (10). Sum is 50 + 10 + 51 = 111.

    Learning route:

    1. Initial state: arr = {30, 50, 10, 0, 0}.
    2. ptr = &arr[0] + 1: &arr[0] is the address of the first element. Adding 1 scales by sizeof(int), so ptr now points to arr[1] (value 50).
    3. a = *ptr: a is assigned the value at ptr, so a = 50.
    4. (*ptr)++: The parentheses force dereferencing first. The value at ptr (which is arr[1]) is incremented to 51. ptr still points to arr[1].
    5. ptr++: The pointer itself is incremented. It now points to the next integer, arr[2] (value 10).
    6. printf("%d", a + (*ptr) + arr[1]):
    • a is 50.
    • *ptr is the value at arr[2], which is 10.
    • arr[1] was modified in step 4 and is now 51.
    • Sum = 50 + 10 + 51 = 111.
    Question 4 · Programming and Data Structures · 2025_Set2 MCQ
    Consider the following C program:

    #include <stdio.h>
    
    void stringcopy(char *, char *);
    
    int main(){
    
            char a[30] = "@#Hello World!";
    
            stringcopy(a, a + 2);
    
            printf("%s\n", a);
    
            return 0;
    
    }
    
    void stringcopy(char *s, char *t) {
    
            while(*t)
    
                   *s++ = *t++;
    
    }

    Which ONE of the following will be the output of the program?
    1. A.

      @#Hello World!

    2. B.

      Hello World!

    3. C.

      ello World!

    4. D.

      Hello World!d!

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: This is a string copy with overlap question, recognizable by passing a and a + 2 to a custom stringcopy function.

    Exam route: The source string starts at index 2 ("Hello World!", length 12). It overwrites the first 12 characters of a. The original characters at indices 12, 13, and 14 ('d', '!', '\0') are never reached by the destination pointer and remain untouched. Result: "Hello World!d!".

    Learning route:

    1. Initial state: a contains "@#Hello World!\0".
    2. stringcopy(a, a + 2) is called. s points to a[0] ('@'), t points to a[2] ('H').
    3. The while(t) loop copies characters from t to s until t is '\0'.
    4. "Hello World!" has 12 characters. The loop runs 12 times, overwriting a[0] through a[11].
    5. The loop terminates when t points to a[14] ('\0'). The assignment s++ = t++ does not execute for the null terminator in this specific loop condition (it stops before copying '\0', but wait, the standard while(t) stops when t is '\0', so '\0' is NOT copied. However, the original '\0' at a[14] remains intact).
    6. Final array content: "Hello World!" (indices 0-11) + "d!\0" (indices 12-14).
    7. printf("%s\n", a) prints "Hello World!d!".
    Question 5 · Programming and Data Structures · 2025_Set1 NAT
    #include <stdio.h>
    void foo(int *p, int x){
    *p=x;
    }
    int main(){
    int *z;
    int a = 20, b = 25;
    z = &a;
    foo(z,b);
    printf("%d",a);
    return 0;
    }

    The output of the given C program is __________. (Answer in integer)
    Correct Answer:

    25.00

    Step-by-Step Solution

    Insight: This is a pointer dereferencing and pass-by-reference simulation question, recognizable by a pointer being passed to a function that modifies the pointed-to value.

    Exam route: z holds &a. foo(z, b) passes &a and 25. Inside foo, *p = x modifies a to 25. printf prints 25.

    Learning route:

    1. a is initialized to 20, b to 25.
    2. z = &a makes z point to a.
    3. foo(z, b) is called. The arguments passed are the address of a and the value 25.
    4. Inside foo, p receives &a and x receives 25.
    5. *p = x dereferences p (accessing a) and assigns it the value of x (25). Thus, a becomes 25.
    6. Back in main, printf("%d", a) prints the updated value of a, which is 25.
    Question 6 · Programming and Data Structures · 2024_Set2 MSQ
    Consider the following C function definition.

    int fX(char *a){
     char *b = a;
     while(*b)
      b++;
     return b - a;}

    Which of the following statements is/are TRUE?
    1. A.

      The function call fX(”abcd”) will always return a value

    2. B.

      Assuming a character array c is declared as char c[] = ”abcd” in main(), the function call fX(c)will always return a value

    3. C.

      The code of the function will not compile

    4. D.

      Assuming a character pointer c is declared as char *c = ”abcd” in main(), the function call fX(c)will always return a value

    Correct Answer:

    ["A","B","D"]

    Step-by-Step Solution

    Insight: This tests string length calculation via pointer subtraction and the validity of passing different string representations to a char * parameter in C.

    Exam route: The function correctly computes length by advancing b until \0 and returning b - a. In C, string literals ("abcd"), character arrays (char c[]), and character pointers (char c) all decay to or are char , making all calls valid and returning 4.

    Learning route:

    1. The function fX takes a char a. It sets b = a and increments b until b is '\0' (false).
    2. It returns b - a, which is the number of characters traversed (the string length).
    3. Option A: fX("abcd"). In C, a string literal is of type char[] (not const char[] as in C++), so it decays to char *. The function reads it safely and returns 4. TRUE.
    4. Option B: char c[] = "abcd"; fX(c);. The array c decays to char * pointing to its first element. The function reads it safely and returns 4. TRUE.
    5. Option C: The code uses standard C pointer arithmetic and dereferencing. It compiles without error. FALSE.
    6. Option D: char c = "abcd"; fX(c);. c is already a char pointing to the string literal. The function reads it safely and returns 4. TRUE.
    Question 7 · Programming and Data Structures · 2024_Set2 MCQ
    What is the output of the following C program?

    #include <stdio.h>

    int main() {
      double a[2]={20.0, 25.0}, *p, *q;
      p = a;
      q = p + 1;
      printf(”%d,%d”, (int)(q – p), (int)(*q – *p));
      return 0;}
    1. A.

      4,8

    2. B.

      1,5

    3. C.

      8,5

    4. D.

      1,8

    Correct Answer:

    B

    Step-by-Step Solution

    Insight: This tests pointer arithmetic rules, specifically that pointer subtraction yields the element count, while dereferenced subtraction yields the value difference.

    Exam route: q - p is the difference between &a[1] and &a[0], which is 1 element. q - p is 25.0 - 20.0 = 5.0, cast to int is 5. Output is 1,5.

    Learning route:

    1. p = a assigns p to the base address of the array, so p points to a[0] (value 20.0).
    2. q = p + 1 advances the pointer by one double element, so q points to a[1] (value 25.0).
    3. q - p: Pointer subtraction automatically divides the byte difference by sizeof(double), yielding the number of elements between them. Since they are adjacent, this is 1.
    4. q - p: Dereferences both pointers to get their values: 25.0 - 20.0 = 5.0.
    5. The (int) cast truncates 5.0 to 5.
    6. The printf outputs 1,5.
    Question 8 · Programming and Data Structures · 2022 MCQ
    What is printed by the following ANSI C program?

    #include<stdio.h>
    int main(int argc, char *argv[])
    {
           int a[3][3][3] =
           {{1, 2, 3, 4, 5, 6, 7, 8, 9},
            {10, 11, 12, 13, 14, 15, 16, 17, 18},
            {19, 20, 21, 22, 23, 24, 25, 26, 27}};
           int i = 0, j = 0, k = 0;
           for( i = 0; i < 3; i++ ){
                  for(k = 0; k < 3; k++ )
                         printf("%d ", a[i][j][k]);
                  printf("\n");
           }
           return 0;
    }
    1. A. 1 2 3

      10 11 12

      19 20 21
    2. B. 1 4 7

      10 13 16

      19 22 25
    3. C. 1 2 3

      4 5 6

      7 8 9
    4. D. 1 2 3

      13 14 15

      25 26 27
    Correct Answer:

    A

    Step-by-Step Solution

    Insight: This is a 3D array traversal question with a fixed middle index, recognizable by the nested loop structure where the middle index variable j is initialized to 0 and never incremented.

    Exam route: Since j=0 always, the loops only access a[i][0][k]. For i=0, k=0,1,2 yields 1, 2, 3. For i=1, yields 10, 11, 12. For i=2, yields 19, 20, 21.

    Learning route:

    1. The array a is initialized as a 3x3x3 block. The first 2D slice (i=0) contains 1-9, the second (i=1) contains 10-18, the third (i=2) contains 19-27.
    2. The outer loop iterates i from 0 to 2.
    3. The inner loop iterates k from 0 to 2.
    4. Crucially, j is initialized to 0 and never changes.
    5. For i=0: prints a[0][0][0], a[0][0][1], a[0][0][2] → 1, 2, 3.
    6. For i=1: prints a[1][0][0], a[1][0][1], a[1][0][2] → 10, 11, 12.
    7. For i=2: prints a[2][0][0], a[2][0][1], a[2][0][2] → 19, 20, 21.
    8. This matches the output in Option A.
    Question 9 · Programming and Data Structures · 2022 MCQ
    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;

    }
    1. A.

      1, 10, 11

    2. B.

      1, 10, 14

    3. C.

      10, 14, 11

    4. D.

      10, 10, 14

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: This is a pointer tracing question with array indexing, recognizable by mixed pointer assignments and arithmetic on array base addresses.

    Exam route: Track x, z[0], and z[1]. p = 10 changes x to 10. (&z[0] + 1) is equivalent to z[1], so z[1] becomes 11 + 3 = 14. z[0] remains 10.

    Learning route:

    1. Initial state: x = 1, z[0] = 10, z[1] = 11.
    2. p = &x: p points to x.
    3. *p = 10: The value at p (which is x) is updated to 10.
    4. p = &z[1]: p is redirected to point to z[1]. (This line does not change z[1]'s value, only p's target).
    5. *(&z[0] + 1) += 3:
    • &z[0] is the address of the first element.
    • Adding 1 scales by the size of int, yielding the address of the next element, &z[1].
    • Dereferencing this gives z[1].
    • z[1] += 3 updates z[1] from 11 to 14.
    1. Final values: x = 10, z[0] = 10, z[1] = 14.
    2. Output matches option D.
    Question 10 · Programming and Data Structures · 2021_Set2 MCQ
    Consider the following ANSI C program.

    #include <stdio.h>
    int main(){
        int arr[4][5];
        int i, j;
        for (i=0; i<4; i++){
            for (j=0; j<5; j++){
                arr[i][j] = 10*i + j;
            }
        }
        printf("%d", *(arr[1] + 9));
        return 0;
    }

    What is the output of the above program?
    1. A.

      14

    2. B.

      20

    3. C.

      24

    4. D.

      30

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: This is a multidimensional array pointer arithmetic question, recognizable by the expression *(arr[1] + 9).

    Exam route: arr[1] is an int pointing to arr[1][0]. Adding 9 moves the pointer 9 integers forward. Since each row has 5 elements, this lands on arr[1 + 1][4] = arr[2][4]. Value is 102 + 4 = 24.

    Learning route:

    1. arr is a 2D array of size 4x5, stored in contiguous row-major memory.
    2. arr[1] is the name of the second row. In an expression, it decays to a pointer to its first element, i.e., &arr[1][0]. Its type is int *.
    3. The expression arr[1] + 9 performs pointer arithmetic. It adds 9 to the int * pointer.
    4. Since each row has 5 elements, moving 5 steps forward reaches the start of the next row: arr[1] + 5 is equivalent to &arr[2][0].
    5. Moving 4 more steps reaches &arr[2][4]. Thus, arr[1] + 9 is equivalent to &arr[2][4].
    6. Dereferencing this with * gives the value of arr[2][4].
    7. The initialization rule is arr[i][j] = 10i + j. For i=2, j=4, the value is 102 + 4 = 24.

    More previous year questions (pyqs) in this unit