Reading — step 1 of 5
Learn
Arrays
A C array is the most honest data structure in programming: N elements of one type, adjacent in memory, and nothing else. No length field, no bounds checking, no methods. Everything higher-level languages give you (and charge you for), you're about to build by hand — starting with knowing where the edges are, because C won't tell you.
Declaration and indexing
int scores[5]; /* 5 ints, side by side — UNINITIALIZED */
int primes[5] = {2, 3, 5, 7, 11}; /* initialized */
int zeros[5] = {0}; /* first explicit, rest zeroed → all 0 */
primes[0] /* 2 — indexing from ZERO */
primes[4] /* 11 — last element is N-1 */
primes[2] = 6;
Zero-based indexing isn't arbitrary in C — a[i] literally means "the memory at a's start plus i element-widths," so the first element is at offset 0. Keep that picture; it becomes load-bearing in the pointers chapter.
The uninitialized rule from chapter 1 applies with interest: int scores[5]; is five slots of garbage. The = {0} idiom zeroes the whole array for free.
The edge: no bounds checking, no length
int a[5] = {1, 2, 3, 4, 5};
printf("%d\n", a[7]); /* compiles. runs. prints… something. */
a[7] = 99; /* writes over some OTHER variable's bytes */
Read or write past the end and C does exactly what you asked: touches the memory that happens to be there. Sometimes it crashes (best case), sometimes it silently corrupts a neighboring variable (worst case: the bug appears three functions away). This is the famous buffer overflow — a top-3 security-vulnerability class for four decades, and you've now written one on purpose in a lesson, which beats writing one by accident in production.
Arrays also don't know their own length. The idiom every C programmer types weekly:
int n = sizeof(a) / sizeof(a[0]); /* total bytes ÷ bytes per element */
— with a caveat you'll meet in the pointers chapter: it only works where the array itself is in scope (arrays passed to functions travel as bare addresses; the length must travel as a separate parameter — which is why every C function signature looks like f(int *arr, int n)).
The loop pattern
Arrays and for are a bonded pair:
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
}
i < n — strictly less — is the boundary discipline: indices run 0 to n−1. Writing i <= n reads one past the end, and per the section above, C will let you.
Your exercise: Largest of Five
Read five numbers into an array, print the largest — the tracker pattern on C terms:
int a[5];
for (int i = 0; i < 5; i++) scanf("%d", &a[i]);
int best = a[0]; /* seed with a real element */
for (int i = 1; i < 5; i++) {
if (a[i] > best) best = a[i];
}
printf("%d\n", best);
Note &a[i] — scanf wants each slot's address. Seeding best from a[0] (rather than 0) survives all-negative input — the test case designed to catch the lazy initialization. Five elements, fixed count, no dynamic anything: exactly the amount of C you know so far, used precisely.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…