Arrays & Strings in C
Array representation, 2D array address calculation, string operations, and common GATE patterns.
Key Points
- ·Arrays are contiguous memory; name decays to pointer to first element
- ·2D array: row-major storage; arr[i][j] at base + (i*cols + j)*sz
- ·String in C: null-terminated char array; "abc" = {a,b,c,\0}
- ·strlen, strcpy, strcat, strcmp, strncpy — library functions
- ·Array passed to function as pointer; size info lost (array decay)
Arrays — A Row of Lockers
Imagine a row of school lockers, all side by side, each with a number on the door. An array is exactly that:
int marks[5] = {80, 75, 90, 60, 85};
Locker: [0] [1] [2] [3] [4]
Value: 80 75 90 60 85
Address: 200 204 208 212 216 (each int = 4 bytes)
Rules:
- All elements are the same type and same size
- They sit next to each other in memory (contiguous)
- The name marks is the address of the first locker (locker 0)
So marks[2] means "go to locker 0's address, skip 2 × 4 = 8 bytes, open that locker" → value 90.
2D Arrays — A Grid of Lockers
A 2D array is like a grid in a spreadsheet. But in memory, it's stored as one long row (row by row).
int mat[3][4]; // 3 rows, 4 columns
How it looks logically: How it sits in memory:
Col: 0 1 2 3
Row 0: a b c d a b c d e f g h i j k l
Row 1: e f g h ↑ ↑ ↑
Row 2: i j k l mat[0][0] mat[1][0] mat[2][0]
This is called row-major order — row 0 comes first, then row 1, then row 2.
Address Formula
To find where mat[i][j] lives:
Address = Base + (i × num_columns + j) × sizeof(element)
Example: mat[2][1] in a 3×4 int matrix, base address = 1000:
Address = 1000 + (2 × 4 + 1) × 4
= 1000 + (8 + 1) × 4
= 1000 + 36
= 1036
Array Decay — The Sneaky Rule
When you pass an array to a function, C secretly converts it to a pointer to the first element. The function has no idea how big the array is.
void print(int arr[], int n) {
// arr is actually int* here
// sizeof(arr) gives sizeof(int*) = 8, NOT the array size!
}
int main() {
int nums[5] = {1,2,3,4,5};
print(nums, 5); // must pass size separately
}
This is why C functions always take a separate n for the array size.
Strings — Arrays with a Secret Ending
A string in C is just a char array with a special marker at the end: the null character \0 (ASCII value 0).
char name[] = "gate";
Stored as: g a t e \0
Index: 0 1 2 3 4
ASCII: 103 97 116 101 0
The null terminator tells string functions "stop here." Without it, functions like printf keep reading memory until they accidentally find a 0 — which causes garbage output or a crash.
char array vs char pointer
char arr[] = "hello"; // copy on stack — YOU OWN this memory, can modify
char *ptr = "hello"; // pointer to READ-ONLY literal in code segment
arr[0] = 'H'; // OK — modifies your own copy
ptr[0] = 'H'; // UNDEFINED BEHAVIOUR — string literal is read-only
Common String Functions (string.h)
| Function | Example | What it does |
|---|---|---|
| strlen(s) | strlen("gate") → 4 | Length (does NOT count \0) |
| strcpy(dst, src) | strcpy(a, "hi") | Copies src into dst |
| strcat(dst, src) | strcat(a, "!") | Appends src to dst |
| strcmp(s1, s2) | strcmp("a","b") → negative | 0 if equal, <0 if s1 |
| strchr(s, c) | strchr("gate", 'a') → pointer to 'a' | First occurrence of char |
Step-by-Step Example: 2D Address Calculation
GATE question style: A 2D array B[4][5] is stored in row-major order. Base address is 2000. Each element is 2 bytes. What is the address of B[3][2]?
Step 1: formula = Base + (i × cols + j) × size
Step 2: = 2000 + (3 × 5 + 2) × 2
Step 3: = 2000 + (15 + 2) × 2
Step 4: = 2000 + 17 × 2
Step 5: = 2000 + 34
Answer: = 2034
Quick Check
Q1. What does strlen("\0hello") return? Answer: 0 — strlen stops at the first \0, which is at index 0.
Q2. Array A[6][8] of integers (4 bytes each), base = 1000, row-major. Address of A[2][3]?
= 1000 + (2 × 8 + 3) × 4 = 1000 + 19 × 4 = 1000 + 76 = 1076
Key Formulas
- Address of arr[i][j] = Base + (i × num_cols + j) × sizeof(element)
- strlen counts characters up to (not including) the null terminator \0
GATE Exam Tips
- ★GATE loves 2D address calculation — always write the formula first, then substitute
- ★strcmp returns 0 when equal, not true/false — watch for != 0 checks
- ★Distinguish char arr[] (stack copy, modifiable) from char *p (literal, read-only)
- ★Array name decays to pointer — sizeof(arr) in function gives pointer size, not array size
Finished reading this topic?
Mark it complete to track your study progress.