Lists → Arrays
Sequential data storage
Introduction
In this lesson, you'll learn about lists → arrays in C. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with sequential data storage.
C has its own approach to sequential data storage, which we'll explore step by step.
The C Way
Let's see how C handles this concept. Here's a typical example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* Fixed array */
int nums[8] = {1, 2, 3, 4, 5};
int len = 5; /* track length manually */
/* "Append" — manual management */
if (len < 8) nums[len++] = 6;
/* Dynamic array */
int *dyn = malloc(8 * sizeof(int));
int dyn_len = 0;
dyn[dyn_len++] = 1;
/* grow: */
dyn = realloc(dyn, 16 * sizeof(int));
free(dyn);
/* Slice: just use pointer + offset */
int *sub = nums + 1; /* points to nums[1] */
/* length of sub is 3 if we track it */
/* Sort with qsort */
int cmp(const void *a, const void *b) {
return *(int*)a - *(int*)b;
}
qsort(nums, len, sizeof(int), cmp);Comparing to Python
Here's how you might have written similar code in Python:
nums = [1, 2, 3, 4, 5]
nums.append(6)
nums.extend([7, 8])
length = len(nums)
sub = nums[1:4]
rev = nums[::-1]
doubled = [n * 2 for n in nums]
# Sort
nums.sort()
nums.sort(reverse=True)
for i, v in enumerate(nums):
print(i, v)You may be used to different syntax or behavior.
C arrays are fixed-size; Python lists grow dynamically
You may be used to different syntax or behavior.
C has no len() — track array length manually or use sizeof trick
You may be used to different syntax or behavior.
No list comprehensions, slicing syntax, or .sort() method in C
You may be used to different syntax or behavior.
C dynamic arrays need malloc/realloc/free; Python's GC handles memory
Step-by-Step Breakdown
1. Fixed vs Dynamic
C arrays are fixed at compile time. For Python-like dynamic growth, use malloc and track the length yourself.
nums = [1, 2, 3]; nums.append(4)int *arr = malloc(capacity * sizeof(int));
arr[len++] = 4; // manual length tracking
if (len == capacity) arr = realloc(arr, 2*capacity*sizeof(int));2. No len() — Track Manually
C arrays don't know their own length. You must track it with a separate variable and pass both the array and its length to functions.
print(len(nums))int n = sizeof(nums) / sizeof(nums[0]); // only for fixed arrays
printf("%d\n", n);3. qsort
C's qsort requires a comparator function. It's more verbose than Python's list.sort() but equally powerful.
nums.sort()int cmp(const void *a, const void *b) {
return *(int*)a - *(int*)b;
}
qsort(nums, len, sizeof(int), cmp);Common Mistakes
When coming from Python, developers often make these mistakes:
- C arrays are fixed-size; Python lists grow dynamically
- C has no len() — track array length manually or use sizeof trick
- No list comprehensions, slicing syntax, or .sort() method in C
Key Takeaways
- Fixed arrays or malloc for dynamic storage
- Track length manually — no len() in C
- No comprehensions, slicing, or append — use loops and manual management
- qsort with comparator function for sorting