Array of strings vs 2D char array
What you'll learn: How to choose between a fixed grid of characters and a flexible list of pointers when storing multiple words.
The "Parking Lot" vs. the "Guest List"
When you want to store a list of strings—like names or commands—you have two main options. Think of a 2D char array as a structured parking lot. Every spot is exactly the same size. If you have five spots that are 20 feet long, it doesn't matter if you park a bicycle or a limousine; that spot still takes up 20 feet of space.
In C, it looks like this: char names[5][20];. You have 5 rows, and every single row reserves 20 bytes of memory. If a name is only "Jo", the other 17 bytes are wasted.
An array of pointers, however, is like a guest list with addresses. The list itself only stores the "location" of where each person is staying. One person might be in a tiny studio, and another in a massive mansion.
Memory Efficiency
The array of pointers (often called an "array of strings") is declared using char *names[]. Instead of a rigid grid, you have a list of memory addresses. Each pointer points to a string located elsewhere in memory.
#include <stdio.h>
int main() {
// 2D Array: A rigid 3x10 grid (30 bytes total)
char grid[3][10] = {"Red", "Green", "Blue"};
// Array of Pointers: Only stores addresses to the actual text
// "Green" takes 6 bytes, "Red" takes 4. No wasted padding!
char *list[] = {"Red", "Green", "Blue"};
printf("Fixed grid: %s\n", grid[1]);
printf("Pointer list: %s\n", list[1]);
return 0;
}
Which one should you use?
Use a 2D array if you need to modify the characters frequently and you know all your strings will be roughly the same length. It keeps everything in one continuous block of memory.
Use an array of pointers when your strings have very different lengths (like a dictionary) or when you want to easily "swap" the order of strings. Swapping two items in a pointer array is instant—you just swap their addresses—whereas swapping items in a 2D array requires copying every single character one by one.
Key takeaway
Use 2D arrays for fixed-size grids and arrays of pointers for flexible, memory-efficient lists of varying lengths.