INTERMEDIATE🔥 High-Yield Exam Topic 4 min read 4 views
C Pointers, Static Storage, Array Decay & Tricky Output Patterns
Precedence of * and ++, 2D array pointer arithmetic, static variables in recursion, and string literal traps.
Essential C Pointer Identities
Given int a[5] = {10, 20, 30, 40, 50};:
adecays to&a[0]of typeint *(steps bysizeof(int)bytes).&ahas typeint (*)[5](pointer to entire array of 5 ints —&a + 1jumps across all 5 elements!).
c
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int *p = (int *)(&a + 1);
printf("%d %d\n", *(a + 1), *(p - 1));
// Output: 20 50
return 0;
}
Static Variables Inside Recursive Functions
🚨 Exam Trap Alert
A static local variable has single storage across all recursive activation frames! If a return expression reads the static variable after a recursive call returns, use the final post-unwinding value of that static variable!
c
int f(int n) {
static int r = 0;
if (n <= 0) return 1;
if (n > 3) {
r = n;
return f(n - 2) + 2;
}
return f(n - 1) + r;
}
// Trace f(5): sets r = 5 -> calls f(3) -> f(2) -> f(1) -> f(0)=1
// Unwinding adds r (=5) three times, then +2: 1 + 5 + 5 + 5 + 2 = 18!
📐 Quick Revision Formula Sheet
2D Array Address (Row-Major, A[R][C])
💡 C always stores multi-dimensional arrays in Row-Major order
#C Programming#Pointers#Recursion#GATE CS
🎯 Practice PYQs on This Topic (1)
Q1.GATE CS C TraceNATMEDIUM
+2 Marks • No NegativeWhat integer value is returned by calling f(5) for the C function int f(int n) { static int r = 0; if (n <= 0) return 1; if (n > 3) { r = n; return f(n - 2) + 2; } return f(n - 1) + r; }?