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

    Pointers, Arrays, Strings and Memory Management notes for GATE CS: 32 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice

    pointers arrays strings and memory management notes

    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.

    The Address-of and Dereference Operators

    The Address-of and Dereference Operators

    Two operators form the backbone of pointer manipulation:

    1. Address-of operator (&): Returns the memory address of a variable.
    2. Dereference operator (*): Accesses or modifies the value stored at the address held by a pointer.
    int x = 10;
        int *p = &x;  // p stores the address of x
    • p evaluates to the address of x.
    • *p evaluates to the value at that address (which is 10).
    • *p = 20; changes the value of x to 20, because p points to x.
    Crucial Rule: The type of the pointer must match the type of the variable it points to. An int * should only point to an int.

    29 more cards in this chapter

    Try a question

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

    Question 1
    Level 1: Warm-up

    Consider the following C declaration:

    ```c

    char msg[] = "OK";

    ```

    What is the numerical value of sizeof(msg)?

    Question 2
    Level 1: Warm-up

    In C programming, a string is terminated by a hidden null character. What is the integer value of this null terminator character \0?

    Question 3
    Level 1: Warm-up

    What is the minimum number of dimensions whose size must be explicitly specified when passing a 2D integer array to a function in C?

    Question 4
    Level 1: Warm-up

    For a 2D array declared as int arr[3][4], what is the total number of contiguous memory locations (in terms of array elements) allocated for this array in C?

    Question 5
    Level 1: Warm-up

    What is the minimum number of asterisks (*) required in the C type declaration of a pointer variable that can directly store the address of the first row of a 2D integer array int arr[3][4]?

    Question 6
    Level 1: Warm-up

    Assertion (A): The argument passed to the malloc function represents the total number of bytes to be allocated.

    Reason (R): The malloc function returns an integer representing the number of bytes successfully allocated, ensuring the unit of measurement is consistent.

    Question 7
    Level 1: Warm-up

    In C, when a dynamic memory allocation request fails due to insufficient heap space, what specific value does the allocation function return?

    Question 8
    Level 1: Warm-up

    Consider the following C code snippet:

    ```c

    int arr[3] = {30, 50, 10};

    int *ptr = &arr[0] + 1;

    ```

    Which array element does ptr point to after this assignment?

    Question 9
    Level 1: Warm-up

    Assertion (A): In a C program, if ptr points to arr[1], executing (*ptr)++ followed by ptr++ will result in ptr pointing to arr[2], and the value of arr[1] being incremented by 1.

    Reason (R): The parentheses in (*ptr)++ force the dereference operation to occur before the increment, modifying the value at the address, whereas ptr++ subsequently advances the pointer address by exactly 1 byte.

    Question 10
    Level 1: Warm-up

    Assertion (A): In the code snippet int arr[5] = {30, 50, 10}; int ptr = &arr[0] + 1;, the expression ptr initially evaluates to 50.

    Reason (R): The expression &arr[0] + 1 adds exactly 1 byte to the base address of the array, which aligns with the start of the second element's memory location.

    Free preview ends here

    Login to view the complete notes

    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 Notes for GATE CS

    Pointers, Arrays, Strings and Memory Management notes for GATE CS: 32 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    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.

    The Address-of and Dereference Operators

    The Address-of and Dereference Operators

    Two operators form the backbone of pointer manipulation:

    1. Address-of operator (&): Returns the memory address of a variable.
    2. Dereference operator (*): Accesses or modifies the value stored at the address held by a pointer.
    int x = 10;
        int *p = &x;  // p stores the address of x
    • p evaluates to the address of x.
    • *p evaluates to the value at that address (which is 10).
    • *p = 20; changes the value of x to 20, because p points to x.
    Crucial Rule: The type of the pointer must match the type of the variable it points to. An int * should only point to an int.

    Pointer Assignment Rules

    Pointer Assignment Rules

    Pointer assignment follows strict type-checking rules to prevent memory corruption.

    Valid Assignments:
    int a = 5;
        int *p1 = &a;    // Valid: address of int assigned to int pointer
        int *p2 = p1;    // Valid: pointer to pointer of same type
    Invalid Assignments:
    int a = 5;
        int *p = a;      // ERROR: assigning an integer value to a pointer
        float *fp = &a;  // WARNING/ERROR: type mismatch (int address to float pointer)
    Key Insight: A pointer variable only stores an address. The type of the pointer tells the compiler how many bytes to read or write when you dereference it.

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

    Question 1 · Programming and Data Structures MCQ

    Consider the following C declaration:

    ```c

    char msg[] = "OK";

    ```

    What is the numerical value of sizeof(msg)?

    1. A.

      2

    2. B.

      3

    3. C.

      4

    4. D.

      8

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is an estimation question testing the difference between the sizeof operator and the strlen function for character arrays.

    Step 1: Identify the declaration. msg is a character array initialized with a string literal.

    Step 2: Determine the contents. The string "OK" consists of two visible characters: 'O' and 'K'.

    Step 3: Apply the string literal rule. The compiler automatically appends a null terminator (\0) to the end of the string literal.

    Step 4: Calculate the total size. The array contains 'O' (1 byte), 'K' (1 byte), and '\0' (1 byte). Total = 3 bytes.

    Answer: B.

    Question 2 · Programming and Data Structures MCQ

    In C programming, a string is terminated by a hidden null character. What is the integer value of this null terminator character \0?

    1. A.

      0

    2. B.

      1

    3. C.

      -1

    4. D.

      255

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a direct recall question about the fundamental definition of a C string.

    Step 1: Recall that C strings are arrays of characters terminated by a null byte.

    Step 2: The null terminator is represented as \0.

    Step 3: The integer (ASCII) value of \0 is exactly 0.

    Answer: A.

    Question 3 · Programming and Data Structures MCQ

    What is the minimum number of dimensions whose size must be explicitly specified when passing a 2D integer array to a function in C?

    1. A.

      0

    2. B.

      1

    3. C.

      2

    4. D.

      3

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a contradiction question testing the pointer decay rule for multidimensional arrays.

    Step 1: Recall that a 2D array int arr[R][C] decays to a pointer to its first row.

    Step 2: The type of this pointer is int (*)[C].

    Step 3: The compiler needs to know C (the number of columns) to calculate the memory offset for row jumps.

    Step 4: The number of rows R can be omitted. Therefore, exactly 1 dimension (the column size) must be specified.

    Answer: B.

    Question 4 · Programming and Data Structures MCQ

    For a 2D array declared as int arr[3][4], what is the total number of contiguous memory locations (in terms of array elements) allocated for this array in C?

    1. A.

      7

    2. B.

      12

    3. C.

      16

    4. D.

      24

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a direct formula question testing the fundamental memory layout of multidimensional arrays.

    Step 1: Recall that in C, a multidimensional array is stored as a single, continuous block of memory.

    Step 2: The total number of elements is the product of all dimension sizes.

    Step 3: Calculate: 3 rows × 4 columns = 12 elements.

    Answer: B.

    Question 5 · Programming and Data Structures MCQ

    What is the minimum number of asterisks (*) required in the C type declaration of a pointer variable that can directly store the address of the first row of a 2D integer array int arr[3][4]?

    1. A.

      0

    2. B.

      1

    3. C.

      2

    4. D.

      3

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a contradiction question testing the pointer decay rule for multidimensional arrays.

    Step 1: Recall the decay rule. When a 2D array name is used in an expression, it decays to a pointer to its first element (which is the first row).

    Step 2: The first row of int arr[3][4] is an array of 4 integers.

    Step 3: Therefore, the array decays to a "pointer to an array of 4 integers".

    Step 4: The C syntax for this type is int (*)[4].

    Step 5: Count the asterisks in int (*)[4]. There is exactly 1 asterisk.

    Answer: B.

    Question 6 · Programming and Data Structures MCQ

    Assertion (A): The argument passed to the malloc function represents the total number of bytes to be allocated.

    Reason (R): The malloc function returns an integer representing the number of bytes successfully allocated, ensuring the unit of measurement is consistent.

    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is not the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a construction question testing the exact signature and behavior of the malloc function.

    Step 1: Evaluate Assertion (A). The syntax is void* malloc(size_t size). The argument size indeed represents the total number of bytes to allocate. Thus, A is true.

    Step 2: Evaluate Reason (R). The malloc function returns a void* (a generic pointer to the first byte of the allocated block), not an integer. It does not return the number of bytes. Thus, R is false.

    Step 3: Since A is true and R is false, the correct choice is C.

    Answer: C.

    Question 7 · Programming and Data Structures MCQ

    In C, when a dynamic memory allocation request fails due to insufficient heap space, what specific value does the allocation function return?

    1. A.

      The integer value 0

    2. B.

      The macro NULL

    3. C.

      The integer value -1

    4. D.

      An uninitialized pointer

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: Standard C dynamic allocation functions (malloc, calloc, realloc) return a null pointer to indicate failure.

    Step 1: Recall the return behavior of allocation functions on failure.

    Step 2: The C standard specifies that they return a null pointer, represented by the macro NULL.

    Step 3: Distinguish NULL from the integer 0 or -1, which are not the standard pointer failure indicators in this context.

    Answer: The macro NULL

    Question 8 · Programming and Data Structures MCQ

    Consider the following C code snippet:

    ```c

    int arr[3] = {30, 50, 10};

    int *ptr = &arr[0] + 1;

    ```

    Which array element does ptr point to after this assignment?

    1. A.

      arr[0]

    2. B.

      arr[1]

    3. C.

      arr[2]

    4. D.

      arr[3]

    Correct Answer:

    B

    Step-by-Step Solution

    Insight: Adding an integer to the address of an array element shifts the pointer by that many elements of the array's type.

    Exam route: &arr[0] is the address of the first element. Adding 1 moves it to the next element, arr[1].

    Learning route:

    Step 1: &arr[0] evaluates to the memory address of the first element of arr.

    Step 2: The + 1 operation is pointer arithmetic. Since ptr is an int , it advances by 1 sizeof(int).

    Step 3: This byte offset perfectly aligns with the memory location of the next integer in the array, which is arr[1].

    Tempting wrong path: Choosing arr[0] assumes the + 1 modifies the value or is ignored. Choosing arr[2] assumes + 1 means "second index" in a 1-based sense, or confuses it with &arr[0] + 2. This breaks the 0-indexed nature of C arrays and the definition of pointer scaling.

    Generalization: &arr[i] + j points to arr[i+j].

    Verification: The value at &arr[0] + 1 is 50, which is exactly the value of arr[1].

    Answer: arr[1]

    Question 9 · Programming and Data Structures MCQ

    Assertion (A): In a C program, if ptr points to arr[1], executing (*ptr)++ followed by ptr++ will result in ptr pointing to arr[2], and the value of arr[1] being incremented by 1.

    Reason (R): The parentheses in (*ptr)++ force the dereference operation to occur before the increment, modifying the value at the address, whereas ptr++ subsequently advances the pointer address by exactly 1 byte.

    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is not the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: Pointer increment (ptr++) advances the pointer by the size of its data type (elements), not by a single byte, even though the postfix ++ operator is used.

    Exam route: Evaluate A: (*ptr)++ increments the value, ptr++ moves to the next element. A is true. Evaluate R: ptr++ advances by 1 byte. This is false; it advances by sizeof(type) bytes. Therefore, A is true, R is false.

    Learning route:

    Step 1: Analyze Assertion (A). (*ptr)++ dereferences first, incrementing the value at arr[1]. Then ptr++ increments the pointer itself, moving it to point to the next element, arr[2]. Assertion (A) is true.

    Step 2: Analyze Reason (R). The first part about parentheses is correct. However, the second part claims ptr++ advances the address by "exactly 1 byte".

    Step 3: Recall that pointer arithmetic scales by the data type size. If ptr is an int *, it advances by 4 bytes, not 1 byte.

    Step 4: Conclude that Reason (R) is false due to this unit mismatch.

    Tempting wrong path: A student might choose Option A, accepting the "1 byte" claim because the ++ operator literally means "add 1". This breaks the rule of scaled pointer arithmetic, confusing the increment of the pointer variable (by 1 element) with the byte offset added to the address.

    Generalization: ptr++ advances the pointer by 1 element, which equals sizeof(type) bytes, never universally 1 byte.

    Verification: If ptr is int * at address 1000, ptr++ results in address 1004, disproving the "1 byte" claim in R.

    Answer: A is true, but R is false.

    Question 10 · Programming and Data Structures MCQ

    Assertion (A): In the code snippet int arr[5] = {30, 50, 10}; int ptr = &arr[0] + 1;, the expression ptr initially evaluates to 50.

    Reason (R): The expression &arr[0] + 1 adds exactly 1 byte to the base address of the array, which aligns with the start of the second element's memory location.

    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is not the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: Pointer arithmetic scales the integer operand by the size of the pointed-to data type, not by a single byte.

    Exam route: Evaluate A: &arr[0] + 1 points to arr[1], so ptr is 50. A is true. Evaluate R: &arr[0] + 1 adds 1 sizeof(int) bytes (4 bytes), not 1 byte. R is false.

    Learning route:

    Step 1: Analyze Assertion (A). &arr[0] is the address of the first element. Adding 1 advances the pointer by one int element, pointing to arr[1]. The value at arr[1] is 50. Assertion (A) is true.

    Step 2: Analyze Reason (R). It claims the expression adds "exactly 1 byte" to the base address.

    Step 3: Recall that pointer arithmetic scales by the data type size. Since ptr is an int *, adding 1 advances the address by sizeof(int) bytes (typically 4 bytes), not 1 byte.

    Step 4: Conclude that Reason (R) is false due to this unit mismatch.

    Tempting wrong path: A student might choose Option A, accepting the "1 byte" claim because the + 1 operator literally means "add 1". This breaks the rule of scaled pointer arithmetic, confusing the increment of the pointer variable (by 1 element) with the byte offset added to the address.

    Generalization: ptr + 1 advances the pointer by 1 element, which equals sizeof(type) bytes, never universally 1 byte.

    Verification: If &arr[0] is 1000, &arr[0] + 1 is 1004, disproving the "1 byte" claim in R.

    Answer: A is true, but R is false.

    More notes in this unit