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:
Address-of operator (&): Returns the memory address of a variable.
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
Which of the following statements regarding C string representation in memory is TRUE?
Question 9
Level 1: Warm-up
Consider the following statements about the C declaration char s[] = "GATE";:
The array s requires exactly 4 bytes of memory.
The expression sizeof(s) evaluates to 5.
The expression strlen(s) evaluates to 4.
The element s[4] is guaranteed to be \0.
How many of the above statements are TRUE?
Question 10
Level 1: Warm-up
Consider a 2D array declared as int arr[3][4]. Which of the following statements about the minimum requirements or boundary conditions for this array are TRUE?
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.
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:
Address-of operator (&): Returns the memory address of a variable.
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 StructuresMCQ
Consider the following C declaration:
```c
char msg[] = "OK";
```
What is the numerical value of sizeof(msg)?
A.
2
B.
3
C.
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 StructuresMCQ
In C programming, a string is terminated by a hidden null character. What is the integer value of this null terminator character \0?
A.
0
B.
1
C.
-1
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 StructuresMCQ
What is the minimum number of dimensions whose size must be explicitly specified when passing a 2D integer array to a function in C?
A.
0
B.
1
C.
2
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 StructuresMCQ
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?
A.
7
B.
12
C.
16
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.
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]?
A.
0
B.
1
C.
2
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 StructuresMCQ
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.
A.
Both A and R are true, and R is the correct explanation of A.
B.
Both A and R are true, but R is not the correct explanation of A.
C.
A is true, but R is false.
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 StructuresMCQ
In C, when a dynamic memory allocation request fails due to insufficient heap space, what specific value does the allocation function return?
A.
The integer value 0
B.
The macro NULL
C.
The integer value -1
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 StructuresMCQ
Which of the following statements regarding C string representation in memory is TRUE?
A.
A string literal assigned to a char * pointer can be safely modified using array subscript notation.
B.
A character array initialized with a string literal stores a mutable copy of the characters plus a null terminator on the stack.
C.
The sizeof operator applied to a char * pointing to a string literal returns the length of the string including the null terminator.
D.
Two identical string literals in a program are guaranteed to be stored at distinct memory addresses.
Correct Answer:
B
Step-by-Step Solution
Key idea: C distinguishes between mutable character arrays and immutable string literals.
Step 1: Option A is false. A char * pointing to a string literal points to read-only memory; modification causes undefined behavior.
Step 2: Option B is true. char arr[] = "..." allocates space on the stack and copies the characters and the null terminator, making it mutable.
Step 3: Option C is false. sizeof on a pointer returns the size of the pointer itself (e.g., 8 bytes), not the string length.
Step 4: Option D is false. Compilers often pool identical string literals, storing them at the same address to save memory.
Answer: Option B is the only true statement.
Question 9 · Programming and Data StructuresNAT
Consider the following statements about the C declaration char s[] = "GATE";:
The array s requires exactly 4 bytes of memory.
The expression sizeof(s) evaluates to 5.
The expression strlen(s) evaluates to 4.
The element s[4] is guaranteed to be \0.
How many of the above statements are TRUE?
Correct Answer:
3.00
Step-by-Step Solution
Key idea: C strings are null-terminated, and character arrays allocate space for the terminator.
Step 1: Analyze statement 1. The string "GATE" has 4 characters plus the hidden \0 terminator, requiring 5 bytes. Statement 1 is FALSE.
Step 2: Analyze statement 2. sizeof(s) returns the total allocated bytes for the array, which is 5. Statement 2 is TRUE.
Step 3: Analyze statement 3. strlen(s) counts characters until \0, excluding the terminator. The length is 4. Statement 3 is TRUE.
Step 4: Analyze statement 4. The indices are s[0]='G', s[1]='A', s[2]='T', s[3]='E', and s[4]='\0'. Statement 4 is TRUE.
Answer: Exactly 3 statements are true.
Question 10 · Programming and Data StructuresMSQ
Consider a 2D array declared as int arr[3][4]. Which of the following statements about the minimum requirements or boundary conditions for this array are TRUE?
A.
The minimum number of dimensions whose size must be explicitly specified when passing arr to a function is 1.
B.
The minimum valid row index for accessing an element is 0.
C.
The array name arr decays to a pointer type that requires the column dimension (4) to be known for arithmetic.
D.
The minimum valid column index for accessing an element is 1.
Correct Answer:
["A","B","C"]
Step-by-Step Solution
Key idea: Multidimensional arrays decay to pointers to their first sub-array, requiring inner dimensions to be known for pointer arithmetic.
Step 1: In C, array indices are 0-based. The minimum valid row index is 0, and the minimum valid column index is 0. Thus, B is true and D is false.
Step 2: When passing a 2D array to a function, the compiler must know the size of the inner dimensions to calculate row jumps. Therefore, the column dimension (4) must be specified. This means at least 1 dimension size must be explicitly provided. Thus, A is true.
Step 3: The array name arr decays to int ()[4], not int or int **. Thus, C is true.