C Programming - Beginner to Brilliance / Arrays and Strings

Array of Strings

(A string array = array where each element is a string)

In C, a string is an array of characters ending with \\0.

So an array of strings is actually a 2D character array.

Think of it like a list of words, where each word is a character array.


✅ Declaration Syntax

char names[3][10];

🔍 What this means:

  • 3 = Number of strings (rows)

  • 10 = Maximum length of each string (columns), including \\0


📌 Example 1: Declaration + Initialization

char fruits[3][10] = {"Apple", "Mango", "Banana"};

Index

Value (String)

0

"Apple"

1

"Mango"

2

"Banana"

Each row is a complete string. Max 9 visible characters + \\0.


🔄 Accessing and Printing Strings

printf("%s", fruits[0]);  // Apple
printf("%s", fruits[1]);  // Mango

🔁 Using Loops to Access All Strings

for(int i = 0; i < 3; i++) {
    printf("Fruit %d: %s\\n", i+1, fruits[i]);
}

🧾 Taking Input for Array of Strings

Let’s take 3 student names from the user:

char students[3][20];

for(int i = 0; i < 3; i++) {
    printf("Enter name %d: ", i+1);
    scanf("%s", students[i]);  // Takes one word only
}
Enter name 1: Alice
Enter name 2: Bob
Enter name 3: Charlie

✅ Explanation:

  • char students[3][20];

    This creates a 2D character array — meaning 3 strings, each of maximum 19 characters + 1 for null terminator \\0.

  • The for loop runs 3 times — asking the user to enter a name each time.

  • scanf("%s", students[i]);

    This reads a single word (string) from input and stores it in the respective row of students.

⚠️ Note: scanf("%s") will stop at a space. For full names, use fgets().


🧰 Behind the Scenes – How It Works in Memory

char lang[3][10] = {"C", "Python", "Java"};

This is a 2D char array like:

📦 Memory Representation of lang[3][10]

🔁 So you get 3 rows (strings) and each has 10 columns (characters)


📍 Alternative: Array of Pointers to Strings

This is a more flexible and memory-efficient approach.

char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

🔍 In this version:

  • days[0] is a pointer to a string literal "Mon"

  • These strings are stored in read-only memory

✅ Loop Through It:

char *days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
for(int i = 0; i < 5; i++) {
    printf("%s\\n", days[i]);
}
Monday
Tuesday
Wednesday
Thursday
Friday

⚔️ Difference: 2D Array vs Array of Pointers

Feature

2D char array (char arr[3][10])

Array of pointers (char *arr[])

Memory

Fixed size

Dynamically points to literals

Flexibility

Less (fixed length)

More (different lengths possible)

Writable

Yes

No (string literals are read-only)

Used for

User input

Constant lists, menus, etc.


❌ Common Mistakes

Mistake

Why It's Wrong

Not reserving space for \\0

Causes unexpected output

Using == to compare strings

Compares addresses, not content

Trying to modify string literal in char *arr[]

Causes crash or undefined behavior


✅ Example: Menu System Using Array of Strings

#include <stdio.h>

int main() {
    char *menu[] = {"Start", "Settings", "Help", "Exit"};

    for(int i = 0; i < 4; i++) {
        printf("%d. %s\\n", i + 1, menu[i]);
    }

    return 0;
}
1. Start
2. Settings
3. Help
4. Exit