notesonly.in

One notebook for every subject — open it anywhere.

Log in

Arrays and Strings: Complete Beginner's Guide For students.

An array is a fixed-size collection of elements of the same type stored in contiguous memory, reached by an index number starting at 0. A string in C is simply a character array with one extra rule: it ends with the special null character '\\0', which marks where the text stops.

Arrays matter because programs handle collections — marks of 100 students, pixels of an image, sensor readings. Without arrays you would need 100 variables and 100 copies of every operation. Strings matter even more: names, messages and passwords are all strings, processed constantly.

Key Concepts

  • Declaration: int marks[5] reserves five integers side by side; valid indexes are 0 to 4.
  • Initialisation: int a[5] = {10, 20, 30, 40, 50}; missing values default to 0.
  • Zero-based indexing: the first element is a[0], the last of five is a[4] — a[5] is out of bounds and causes undefined behaviour; C will not stop you.
  • 2D arrays: int m[3][4] is 3 rows × 4 columns, stored row by row; traversed with nested loops.
  • String anatomy: char name[20] = "Asha" stores 'A','s','h','a','\\0' — the '\\0' terminator is what makes it a string rather than a plain char array.
  • string.h essentials: strlen (length excluding '\\0'), strcpy (copy — destination must be big enough), strcat (join), strcmp (0 if equal).

How It Works

How strlen finds the length of "Hello":

  1. Memory holds: 'H' 'e' 'l' 'l' 'o' '\\0' at indexes 0–5.
  2. strlen starts a counter at 0 and walks index by index.
  3. Each non-'\\0' character bumps the counter: H→1, e→2, l→3, l→4, o→5.
  4. At index 5 it meets '\\0' and stops, returning 5 — the terminator itself is never counted.

Worked Example

Sum the elements of int a[] = {10, 20, 30, 40, 50}:

int sum = 0;<br>for (int i = 0; i &lt; 5; i++) sum = sum + a[i];

  1. i = 0 → sum = 0 + a[0] = 0 + 10 = 10
  2. i = 1 → sum = 10 + a[1] = 10 + 20 = 30
  3. i = 2 → sum = 30 + a[2] = 30 + 30 = 60
  4. i = 3 → sum = 60 + a[3] = 60 + 40 = 100
  5. i = 4 → sum = 100 + a[4] = 100 + 50 = 150; i = 5 fails the condition, loop ends.

Final sum = 150. This walk-every-index-and-accumulate pattern underlies searching, averaging and maximum-finding in any array.

Common Misconceptions

  • "Arrays check their own bounds." C does not — reading a[10] of a 5-element array silently reads garbage or crashes; the programmer is the safety net.
  • "Strings and char arrays are identical." Almost — but only the '\\0'-terminated one is a string; printf("%s") on a non-terminated array prints garbage until it accidentally hits a zero byte.
  • "You can assign one array to another with =." No — a = b is illegal for arrays; use a loop or memcpy, and strcpy for strings.

Real-World Applications

  • Images and audio: a photo is a 2D array of pixels; a sound clip is a 1D array of samples.
  • Databases: tables are arrays of records; indexes are arrays of keys.
  • Text processing: editors, search engines and chat apps are string manipulation at massive scale.
  • Embedded sensors: microcontrollers buffer readings in fixed arrays before transmitting them.

Arrays store same-type values in contiguous memory with 0-based indexing; strings are char arrays terminated by '\\0'. Index carefully, respect the terminator, lean on string.h — and they become the workhorses of every program you write.