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

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

    pointers arrays strings and memory management short notes

    Pointer Rules Checklist

    Pointer Rules Checklist

    • Definition: A pointer variable stores a memory address.
    • Type Matching: int * must point to int. Mismatches cause warnings or errors.
    • Arithmetic Scaling: advances the address by .
    • Pointer Subtraction: yields the number of elements between them, not bytes.
    • Precedence: *p++ increments the pointer; (*p)++ increments the value.
    • Safety: Never dereference an uninitialized pointer or NULL.
    Memory Hook:
    • & means "Where is it?" (Address)
    • * means "What is there?" (Value)

    Quick Recap: Strings and Pointers

    Quick Recap: Strings and Pointers

    1. Termination

    All C strings end with \0.

    2. Storage

    char arr[] = "..." is Mutable (Stack).
    char *ptr = "..." is Immutable (Read-only).

    3. Traversal

    while(*ptr) ptr++ is the standard idiom.

    4. Arithmetic

    end_ptr - start_ptr gives the length.

    5. Overlap

    Copying from lower to higher address (src > dest) is safe for simple loops, but watch for untouched trailing characters. Copying src < dest requires backward iteration or a temporary buffer.

    Quick Recap: Multidimensional Arrays

    Quick Recap: Multidimensional Arrays

    1
    Memory Layout: Strictly row-major, contiguous block.
    2
    Address Math: Requires inner dimension sizes ( for 2D, and for 3D).
    3
    Decay Rule: int arr[R][C] decays to int (*)[C], never int **.
    4
    Equivalence: arr[i][j] is exactly *(*(arr + i) + j).
    5
    Pointer Jumps: arr + 1 jumps by bytes, not .

    1 more card 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 short 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 Short Notes for GATE CS

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

    Pointer Rules Checklist

    Pointer Rules Checklist

    • Definition: A pointer variable stores a memory address.
    • Type Matching: int * must point to int. Mismatches cause warnings or errors.
    • Arithmetic Scaling: advances the address by .
    • Pointer Subtraction: yields the number of elements between them, not bytes.
    • Precedence: *p++ increments the pointer; (*p)++ increments the value.
    • Safety: Never dereference an uninitialized pointer or NULL.
    Memory Hook:
    • & means "Where is it?" (Address)
    • * means "What is there?" (Value)

    Quick Recap: Strings and Pointers

    Quick Recap: Strings and Pointers

    1. Termination

    All C strings end with \0.

    2. Storage

    char arr[] = "..." is Mutable (Stack).
    char *ptr = "..." is Immutable (Read-only).

    3. Traversal

    while(*ptr) ptr++ is the standard idiom.

    4. Arithmetic

    end_ptr - start_ptr gives the length.

    5. Overlap

    Copying from lower to higher address (src > dest) is safe for simple loops, but watch for untouched trailing characters. Copying src < dest requires backward iteration or a temporary buffer.

    Quick Recap: Multidimensional Arrays

    Quick Recap: Multidimensional Arrays

    1
    Memory Layout: Strictly row-major, contiguous block.
    2
    Address Math: Requires inner dimension sizes ( for 2D, and for 3D).
    3
    Decay Rule: int arr[R][C] decays to int (*)[C], never int **.
    4
    Equivalence: arr[i][j] is exactly *(*(arr + i) + j).
    5
    Pointer Jumps: arr + 1 jumps by bytes, not .

    Quick Revision Checklist

    Quick Revision Checklist

    Core Rules

    1. Heap vs Stack: Dynamic = Heap, Local = Stack.
    2. Malloc: malloc(bytes), returns void*, garbage data.
    3. Calloc: calloc(count, size), returns void*, zeroed data.
    4. Realloc: realloc(ptr, new_size), may move memory. Use temp pointer.
    5. Free: free(ptr), returns memory. Set ptr = NULL.
    6. Safety: Always check if (ptr == NULL) after allocation.

    Key Errors to Avoid

    • Memory Leak: losing address without free.
    • Dangling Pointer: using freed memory.
    • Double Free: freeing same block twice.
    • Buffer Overflow: writing beyond allocated size.

    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 short notes in this unit