Programming & DSA
Pointers & Arrays
Pointers, arrays, strings, and structures are the highest-yield C area in GATE: learn pointer scaling, array decay, and sizeof rules and trace memory carefully.
GATE CSE pointers and arrays in C — pointer arithmetic, arrays vs pointers, strings, structures and unions, 2D array addressing, and sizeof traps with samples and drills.
Learning outcomes
- Predict the result of pointer arithmetic and dereference expressions with correct scaling by element size
- Distinguish arrays from pointers, including array decay and the sizeof traps inside functions
- Handle C strings, structures, padding, and unions, including their sizes
- Compute addresses in 1D and 2D arrays (row-major) and read pointer-to-array declarations
Pointer basics
- A pointer variable stores the address of another object. & gives the address of a variable and * dereferences (follows) a pointer. For int x, *p = &x: *p and x name the same memory.
- The type of the pointer decides how many bytes are read when it is dereferenced and how far p+1 moves. Pointer size is the same for all pointer types on a given machine (commonly 4 bytes on 32-bit, 8 bytes on 64-bit); use the size stated in the question.
- Pointer to pointer: int **q = &p; then **q is x. Each * peels off one level of indirection.
- NULL (address 0) must never be dereferenced. A dangling pointer points to freed or out-of-scope memory; a wild pointer is uninitialised.
- malloc(n) returns n bytes from the heap (void*, cast optional in C); free() releases it. Forgetting free causes a memory leak; using memory after free causes dangling access.
- void * can hold any object address but cannot be dereferenced or incremented without a cast (GATE treats arithmetic on void* as invalid unless stated).
Worked example
Problem. int x = 10, *p = &x, **q = &p; What is **q + 1?
Solution. **q is x = 10, so the value is 11.
Pointer arithmetic and arrays
- p + k moves the address by k * sizeof(*p) bytes, not k bytes. For a double* at address 1000, p + 3 is 1000 + 3×8 = 1024.
- Subtracting two pointers into the same array gives the number of ELEMENTS between them (type ptrdiff_t), not bytes. Adding two pointers is illegal.
- a[i] is defined as *(a + i), so i[a] also works. The array name a converts (decays) to &a[0] in most expressions, but an array name is NOT a modifiable lvalue: a++ is an error while p++ is fine.
- sizeof(a) for int a[10] is 10 × sizeof(int) (e.g., 40) and sizeof(a)/sizeof(a[0]) gives the element count — but only in the scope where a is a real array.
- Inside a function, void f(int a[]) and void f(int *a) are identical: a is a pointer, so sizeof(a) is the pointer size, not the array size.
- &a has type "pointer to the whole array", so &a + 1 jumps over the entire array: (int*)(&a + 1) - a equals the number of elements of a.
- Precedence traps: *p++ increments the pointer after reading (postfix ++ binds tighter than *), (*p)++ increments the value, *++p increments first then reads.
Formulas to remember
-
p + k → address + k × sizeof(*p) -
a[i] ≡ *(a + i) -
p − q → (address_p − address_q) / sizeof(*p) elements
Worked example
Problem. int a[] = {1, 2, 3, 4, 5}; int *p = a + 1; What is p[2]?
Solution. p points to a[1]; p[2] = *(p + 2) = a[3] = 4.
Strings in C
- A C string is a char array ending with the null character (value 0). The literal "GATE" occupies 5 bytes: G A T E and the terminator.
- strlen counts characters up to (not including) the null; sizeof of a char array counts all bytes including the null. char s[] = "GATE": sizeof(s) = 5, strlen(s) = 4.
- char s[] = "abc" creates a modifiable array copy. char *p = "abc" points to a string literal that must not be modified (undefined behaviour, usually a segmentation fault).
- Comparing strings with == compares addresses, not contents. Use strcmp(a, b) == 0 for equality (strcmp returns 0 when equal, negative if a < b, positive if a > b).
- A null embedded in an array stops C string functions: strlen("GATE\0CS") = 4 although the array is longer.
- p + 3 for char *p = "abcdef" points to "def"; printf("%s", p + 3) prints def. Functions like strcpy and strcat do not check bounds — the destination must be large enough.
| Expression | Meaning | Value for "GATE" |
|---|---|---|
| sizeof(s) | Bytes in char array s[] | 5 |
| strlen(s) | Chars before the null | 4 |
| s[4] | Terminator | 0 (null char) |
| s + 2 | Address of third char | prints "TE" |
Worked example
Problem. char *s = "abcdef"; printf("%s", s + 3); What is printed?
Solution. s + 3 points to the character d, so the string printed from there is def.
Structures and unions
- A struct groups members laid out in order. Members are aligned to their natural boundary, so padding bytes may be inserted, and the total size is rounded to a multiple of the largest member alignment.
- Typical sizes with 4-byte int alignment: struct { char c; int i; } is 8 bytes (1 + 3 padding + 4); struct { char c; int i; char d; } is 12 bytes; reordering members as int, char, char gives 8 bytes.
- Access with . for a variable and -> for a pointer: p->x is (*p).x. Assigning one struct variable to another copies all members (including arrays inside).
- A union stores all members at the same address; its size is the size of the largest member (rounded for alignment). Writing one member overwrites the others.
- A self-referential struct needs a pointer to itself (struct node { int d; struct node *next; }); a struct cannot contain itself directly.
- Structures can be passed by value (copied) or by pointer; passing by pointer avoids copying large structures.
| Declaration | Layout | sizeof (4-byte int, 8-byte double) |
|---|---|---|
| struct { char c; int i; } | c, pad 3, i | 8 |
| struct { char c; int i; char d; } | c, pad 3, i, d, pad 3 | 12 |
| struct { int i; char c; char d; } | i, c, d, pad 2 | 8 |
| union { int i; char c[6]; double d; } | max member, aligned to 8 | 8 |
Worked example
Problem. struct S { int x; char y; }; What is sizeof(struct S) with 4-byte int alignment?
Solution. x occupies 4 bytes, y 1 byte, then 3 bytes of padding so the size is a multiple of 4: total 8.
2D arrays and sizeof traps
- C stores 2D arrays in row-major order. For int a[R][C] with base address B and element size w: address of a[i][j] = B + (i × C + j) × w.
- a[i][j] is *(*(a + i) + j). Here a + i advances by a whole row (C × w bytes) because a[i] is itself an array of C elements.
- sizeof(a) = R × C × w, sizeof(a[0]) = C × w (one row), sizeof(a[0][0]) = w.
- int *p[5] is an array of 5 pointers to int. int (*p)[5] is a single pointer to an array of 5 ints; p + 1 moves 5 × sizeof(int) bytes.
- To pass a 2D array to a function, the column size must be known: void f(int a[][4]) or void f(int (*a)[4]). Inside, sizeof(a) is only a pointer size.
- Sizeof traps: sizeof on a pointer is the pointer size; sizeof("abc") = 4; sizeof(char) = 1 always; sizeof does not evaluate its operand (sizeof(i++) leaves i unchanged); sizeof returns an unsigned type.
- Column-major (Fortran style) formula for comparison: address = B + (j × R + i) × w.
Formulas to remember
-
Row-major: &a[i][j] = B + (i × C + j) × w -
Column-major: &a[i][j] = B + (j × R + i) × w -
sizeof(a[R][C]) = R × C × w
Worked example
Problem. int a[3][4], base address 1000, int = 4 bytes. Address of a[2][1]?
Solution. 1000 + (2 × 4 + 1) × 4 = 1000 + 36 = 1036.
GATE exam tricks — pointers, arrays and strings
- Draw boxes first: write the array, its addresses (base, base + w, base + 2w ...) and where every pointer points before you evaluate anything. Most wrong answers come from skipping this.
- Decay exceptions: an array name does NOT decay under sizeof, under unary &, or when it is a string literal initialiser of a char array. Everywhere else it becomes &a[0].
- Precedence ladder for pointers: [] and postfix ++ bind tighter than *, so *p++ is *(p++), and *p[2] is *(p[2]). Parenthesise before evaluating.
- p[-1] is legal as long as it stays inside the array: if p = a + 2 then p[-1] is a[1].
- Array-of-pointers vs pointer-to-array: int *p[3] vs int (*p)[3]. Read declarations inside-out: start at the name, go right ([]), then left (*).
- Printing a pointer difference uses element units: &a[5] - &a[2] is 3 even though the byte distance is 12 for 4-byte ints.
- String functions: strcpy and strcat need destination space for the terminator; strcmp returns 0 on equal, so if (strcmp(a, b)) is TRUE when the strings DIFFER.
- For the unsigned-comparison trap, sizeof returns an unsigned value: if (sizeof(a) - 100 > 0) is almost always true because the subtraction wraps.
Worked example
Problem. int a[] = {10, 20, 30, 40}; int *p = a; int x = *p++; int y = *p; int z = (*p)++; What are x, y, z and a[1] afterwards?
Solution. x = *p++ reads 10, then p moves to a[1]. y = *p = 20. z = (*p)++ yields 20 and increments the VALUE a[1] to 21. So x = 10, y = 20, z = 20, a[1] = 21. (Never read and modify the same object in one expression — that is undefined behaviour and GATE avoids it.)
Worked trace: pointer walk and struct size
- Trace one statement per line and keep a small table with columns p (as index into a), *p and the array contents.
- Pointer swap of values uses *: swapping p and q only exchanges the local pointer copies; it does not change the pointed-to data.
- For struct size, list members with offsets: place each member at the next multiple of its own alignment, then round the final size to the largest member alignment.
Worked example
Problem. struct T { char a; double b; char c; }; with 8-byte alignment for double. What is sizeof(struct T)?
Solution. a at offset 0 (1 byte), then 7 bytes of padding so b starts at 8; b occupies 8..15; c at 16, then padding to a multiple of 8: total 24. Reordering as double, char, char gives 16.
Exam tips
- Always scale pointer arithmetic by the element size; draw the memory as a row of boxes.
- sizeof on an array parameter gives the pointer size — a classic trap.
- For strings, count the null in sizeof but not in strlen.
- For structs, lay members out byte by byte and add padding before aligning the total size.
- Write the row-major address formula first for any 2D array question.
10 GATE-style MCQs for Pointers & Arrays with full explanations. Attempt each question before revealing the answer.
int a[] = {10, 20, 30, 40}; int *p = a; printf("%d", *(p + 2)); prints:
Show answer & explanation
Answer: B
*(p + 2) = a[2] = 30.
With 4-byte int, what is sizeof(a) for int a[10] declared in the same function?
Show answer & explanation
Answer: C
sizeof array = number of elements × element size = 10 × 4 = 40.
void f(int a[]) { printf("%d", sizeof(a)); } is called as f(arr) where int arr[10]. On a machine with 8-byte pointers, the output is:
Show answer & explanation
Answer: B
The parameter a is really an int*, so sizeof(a) is the pointer size, 8 bytes.
char s[] = "GATE"; printf("%d %d", sizeof(s), strlen(s)); prints:
Show answer & explanation
Answer: B
sizeof counts the null terminator (5 bytes); strlen counts characters before it (4).
int a[3][4] has base address 2000 and int is 4 bytes. The address of a[2][3] in row-major order is:
Show answer & explanation
Answer: B
2000 + (2 × 4 + 3) × 4 = 2000 + 44 = 2044.
int a[10]; int *p = &a[7], *q = &a[2]; What is p - q?
Show answer & explanation
Answer: A
Pointer subtraction yields the number of elements between them: 7 − 2 = 5 (not bytes).
With 4-byte int alignment, sizeof(struct S) for struct S { char c; int i; char d; }; is:
Show answer & explanation
Answer: C
c at offset 0, 3 padding bytes, i at offset 4, d at offset 8, then 3 padding bytes to align the size to 4: total 12.
With 4-byte int and 8-byte double, sizeof(union U) for union U { int i; char c[6]; double d; }; is:
Show answer & explanation
Answer: B
A union is as large as its largest member: double is 8 bytes (c[6] needs 6, int needs 4). Alignment of 8 gives total 8.
int a[5]; int *p = (int *)(&a + 1); What is p - a?
Show answer & explanation
Answer: C
&a has type pointer-to-int[5], so &a + 1 points just past the whole array. Cast to int*, the difference from a is 5 elements.
char *p = "hello"; p[0] = 'H'; What does the standard say about this statement?
Show answer & explanation
Answer: B
Modifying a string literal is undefined behaviour and typically causes a segmentation fault. Use char p[] = "hello" for a modifiable copy.
Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.
int a[] = {5, 6, 7}; printf("%d", *a + 1); prints:
Show answer & explanation
Answer: B) 6
*a is a[0] = 5, then + 1 gives 6 (dereference binds tighter than +).
int a[] = {5, 6, 7}; int *p = a; p++; printf("%d", *p); prints:
Show answer & explanation
Answer: B) 6
After p++ the pointer references a[1] = 6.
char s[] = "abc"; printf("%d", sizeof(s)); prints:
Show answer & explanation
Answer: B) 4
The array holds a, b, c and the null terminator: 4 bytes.
char *s = "abcdef"; printf("%s", s + 3); prints:
Show answer & explanation
Answer: B) def
s + 3 points to d, and %s prints until the null: def.
int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; printf("%d", *(*(a + 1) + 2)); prints:
Show answer & explanation
Answer: C) 6
a + 1 is row 1; *(a + 1) + 2 is &a[1][2]; dereferencing gives 6.
With 4-byte int, what is sizeof(a[0]) for int a[2][3]?
Show answer & explanation
Answer: B) 12
a[0] is one row of 3 ints: 3 × 4 = 12 bytes.
double *p holds address 1000 and double is 8 bytes. What is the value of p + 3?
Show answer & explanation
Answer: C) 1024
1000 + 3 × 8 = 1024.
With 4-byte int alignment, sizeof(struct { int x; char y; }) is:
Show answer & explanation
Answer: B) 8
x takes 4 bytes, y takes 1, and 3 bytes of padding make the size a multiple of 4: 8.
int x = 10, *p = &x, **q = &p; printf("%d", **q + 1); prints:
Show answer & explanation
Answer: B) 11
**q = x = 10, so **q + 1 = 11.
Which expression tests whether two C strings s1 and s2 have the same contents?
Show answer & explanation
Answer: B) strcmp(s1, s2) == 0
strcmp returns 0 for equal contents; == compares addresses, strlen compares only lengths, and *s1 == *s2 compares only the first characters.
Topic FAQs
Are arrays and pointers the same in C?
No. An array name decays to a pointer to its first element in most expressions, but an array owns its storage, has a size given by sizeof, and cannot be reassigned; a pointer is a variable holding an address.
Why is sizeof(arr) wrong inside a function receiving the array?
The parameter is really a pointer, so sizeof gives the pointer size and not the array size.
Why does the compiler add padding to structures?
To align each member to its natural boundary so the hardware can access it efficiently.
What is the difference between char s[] = "abc" and char *s = "abc"?
The first is a modifiable array copy of 4 bytes; the second is a pointer to a read-only literal, and modifying the literal is undefined behaviour.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.