C in 100 Seconds: Arrays of Pointers | Episode 28
Video: C in 100 Seconds: Arrays of Pointers — String Lists and Pointer Tables | Episode 28 by Taught by Celeste AI - AI Coding Coach
Watch full page →Arrays of Pointers — String Lists and Pointer Tables
C in 100 Seconds, Episode 28
An array of pointers holds addresses instead of values. Each element points somewhere else in memory. This is how C handles lists of strings, command-line arguments, and dispatch tables.
Array of String Pointers
The most common use — a list of strings:
char *colors[] = {"red", "green", "blue"};
Each element is a char* pointing to a string literal. The array itself holds three pointers, not three copies of the strings.
Iterating
The sizeof trick works the same way — total size divided by one element:
int len = sizeof(colors) / sizeof(colors[0]);
for (int i = 0; i < len; i++) {
printf("colors[%d] = %s\n", i, colors[i]);
}
Array of Int Pointers
You can also store pointers to individual variables:
int a = 10, b = 20, c = 30;
int *nums[] = {&a, &b, &c};
Each element holds an address. Dereference with *nums[i] to get the value.
Why This Matters
- argv — command-line arguments are
char *argv[] - String tables — menus, error messages, configuration
- Dispatch tables — arrays of function pointers for routing
- Dynamic collections — arrays of malloc'd pointers
Full Code
#include <stdio.h>
int main() {
char *colors[] = {"red", "green", "blue"};
int len = sizeof(colors) / sizeof(colors[0]);
for (int i = 0; i < len; i++) {
printf("colors[%d] = %s\n", i, colors[i]);
}
int a = 10, b = 20, c = 30;
int *nums[] = {&a, &b, &c};
for (int i = 0; i < 3; i++) {
printf("*nums[%d] = %d\n", i, *nums[i]);
}
return 0;
}
Next episode: Double Pointers — pointer to pointer, dynamic 2D arrays.
Student code: github.com/GoCelesteAI/c-in-100-seconds/episode28