Skip to content
StudyFame

Array of Strings in C

One string holds one name. But a class register has many names, a menu has many dishes, and a quiz has many options. For a list of strings, C uses an array of strings.

In this lesson you will store, print, search and sort names alphabetically. You will also learn the two ways to make a list of strings and when to use each.

What You Will Learn

  • What an array of strings is.
  • How to declare it as a 2D char array.
  • How to read and print a list of names.
  • How to search for a name.
  • How to sort names in alphabetical order.
  • 2D char array vs array of char pointers.

What is an Array of Strings in C?

Exam definition: An array of strings is a two-dimensional character array in which each row stores one string. In char names[R][C], R is the number of strings and C is the maximum length of each string plus one (for '\0').

Memory trick: Think of a register notebook: each line (row) holds one name, and every line has the same width. names[i] = the i-th line; names[i][j] = the j-th letter on that line.

Syntax of an Array of Strings

char names[5][20];                       /* 5 strings, each up to 19 letters */

char days[3][10] = {"Mon", "Tue", "Wed"};

char *fruits[] = {"Apple", "Mango", "Banana"};   /* array of pointers */
char days[3][10] = {"Mon", "Tue", "Wed"};

          0    1    2    3    4 ...  9
days[0]: 'M'  'o'  'n' '\0' '\0' ... '\0'
days[1]: 'T'  'u'  'e' '\0' '\0' ... '\0'
days[2]: 'W'  'e'  'd' '\0' '\0' ... '\0'
  • days[1] is a whole string: "Tue". Print it with %s.
  • days[1][0] is one character: 'T'. Print it with %c.
  • Memory used = 3 × 10 = 30 bytes, even though the words are short.

Example 1: Read and Print a List of Names

#include <stdio.h>

int main(void)
{
    char names[10][20];
    int n, i;

    printf("How many students? ");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        printf("Name %d: ", i + 1);
        scanf("%19s", names[i]);       /* names[i] is one row: no & */
    }

    printf("\nClass list:\n");
    for (i = 0; i < n; i++)
    {
        printf("%d. %s (starts with %c)\n", i + 1, names[i], names[i][0]);
    }

    return 0;
}

Output

How many students? 3
Name 1: Asha
Name 2: Ravi
Name 3: Neha

Class list:
1. Asha (starts with A)
2. Ravi (starts with R)
3. Neha (starts with N)

Code Explanation

  1. char names[10][20]; makes room for 10 names of up to 19 letters each.
  2. names[i] is row i, which is itself a char array, so scanf("%19s", names[i]) needs no &.
  3. Printing names[i] with %s shows the whole name; names[i][0] with %c shows its first letter.
  4. To read names with spaces (like "Ravi Kumar"), use fgets(names[i], 20, stdin) as you learnt in String Input and Output.

How Sorting Names Works

Sorting strings is the same bubble sort you learnt for numbers, with two changes:

NumbersStrings
if (a[j] > a[j+1])if (strcmp(s[j], s[j+1]) > 0)
temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;strcpy(temp, s[j]); strcpy(s[j], s[j+1]); strcpy(s[j+1], temp);

You can't swap rows with =, because arrays can't be assigned. Use strcpy three times, with a char temp[20].

Example 2: Sort Names Alphabetically and Search a Name

#include <stdio.h>
#include <string.h>

#define N 5

int main(void)
{
    char names[N][20] = {"Rohan", "Anjali", "Vikram", "Meena", "Deepak"};
    char temp[20];
    char key[] = "Meena";
    int i, j, found = -1;

    /* bubble sort using strcmp */
    for (i = 0; i < N - 1; i++)
    {
        for (j = 0; j < N - 1 - i; j++)
        {
            if (strcmp(names[j], names[j + 1]) > 0)
            {
                strcpy(temp, names[j]);
                strcpy(names[j], names[j + 1]);
                strcpy(names[j + 1], temp);
            }
        }
    }

    printf("Sorted names:\n");
    for (i = 0; i < N; i++)
        printf("  %s\n", names[i]);

    /* linear search */
    for (i = 0; i < N; i++)
    {
        if (strcmp(names[i], key) == 0)
        {
            found = i;
            break;
        }
    }
    if (found != -1)
        printf("%s found at position %d\n", key, found + 1);
    else
        printf("%s not found\n", key);

    return 0;
}

Output

Sorted names:
  Anjali
  Deepak
  Meena
  Rohan
  Vikram
Meena found at position 3
  • strcmp(names[j], names[j + 1]) > 0 means the first name comes later in dictionary order, so they swap.
  • strcmp uses ASCII, so capitals come before small letters ("Zoya" before "amit"). Keep the same case for correct order.
  • The search compares with strcmp(...) == 0, never ==.

2D Char Array vs Array of Char Pointers

char a[3][8] = {"Hi", "Hello", "Hey"};      char *p[3] = {"Hi", "Hello", "Hey"};

a: |H|i|\0| | | | | |   fixed 8 each     p[0] --> "Hi"
   |H|e|l|l|o|\0| | |   = 24 bytes       p[1] --> "Hello"   (strings stored elsewhere,
   |H|e|y|\0| | | | |                    p[2] --> "Hey"      read-only)
Pointchar a[R][C]char *p[R]
MemoryR × C bytes, some wastedJust enough for each string + pointers
Change letters?YesNo (literals are read-only)
Read input into it?Yes, directlyOnly after giving each pointer memory
Swap two strings3 × strcpyJust swap the pointers (fast)
Best forUser input, editable listsFixed lists: menus, month names

You will understand pointers fully in Module 10.

Important Points to Remember

  • An array of strings is a 2D char array: one string per row.
  • Column size must include space for '\0'.
  • names[i] = whole string; names[i][j] = one character.
  • No & when reading names[i] with scanf.
  • Compare with strcmp, swap with strcpy.
  • char *p[] saves memory but its literals are read-only.

Common Mistakes Beginners Make

MistakeWhat happensFix
names[j] = names[j+1];Compile error: array not assignableUse strcpy
if (names[i] == key)Compares addresses: never truestrcmp(names[i], key) == 0
Column size = longest name lengthNo room for '\0'Length + 1
scanf("%s", p[i]) with char *p[5];Crash: pointer has no memoryUse char p[5][20]
Mixed case while sorting"Zoya" sorted before "amit"Use one case, or compare lowercase copies

Array of Strings Practice Questions

  1. Declare an array that can hold 50 city names of up to 24 letters.
  2. For char d[3][10] = {"Mon", "Tue", "Wed"};, what is d[2][1]?
  3. Why can't we swap two names using =?
  4. How many bytes does char s[4][15] take?
  5. Give one advantage of char *p[] over char a[][].
Show answers
  1. char city[50][25];
  2. 'e' (from "Wed").
  3. Arrays can't be assigned in C; we must copy characters with strcpy.
  4. 4 × 15 = 60 bytes.
  5. It uses only the memory each string needs, and swapping is just a pointer swap.

Coding Exercise

Store the subjects "Maths", "Physics", "C", "Electronics" in an array of strings. Print the longest subject name and its length.

Show solution
#include <stdio.h>
#include <string.h>

int main(void)
{
    char sub[4][20] = {"Maths", "Physics", "C", "Electronics"};
    int i, longest = 0;

    for (i = 1; i < 4; i++)
    {
        if (strlen(sub[i]) > strlen(sub[longest]))
            longest = i;
    }
    printf("Longest: %s (%zu letters)\n", sub[longest], strlen(sub[longest]));
    return 0;
}
Longest: Electronics (11 letters)

Quick Quiz: Array of Strings in C (MCQ)

  1. An array of strings is usually a:

    A) 1D int array   B) 2D char array   C) 3D array   D) float array

  2. In char s[5][12], the maximum letters per string is:

    A) 12   B) 11   C) 5   D) 60

  3. Correct way to read the i-th name:

    A) scanf("%s", &s[i][0][0]);   B) scanf("%s", s[i]);   C) scanf("%c", s[i]);   D) scanf("%s", s);

  4. To swap two rows of a 2D char array we use:

    A) =   B) strcat   C) strcpy   D) strlen

  5. What is m[1] in char *m[] = {"Jan", "Feb", "Mar"};?

    A) 'F'   B) "Feb"   C) "Jan"   D) 'e'

Show answers
  1. B: 2D char array.
  2. B: 11 (one place for '\0').
  3. B: scanf("%s", s[i]);
  4. C: strcpy.
  5. B: "Feb".

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Array of strings2D char array, one string per row
char s[R][C]R strings, each up to C − 1 letters
s[i] / s[i][j]String / character
Read rowscanf("%s", s[i]), no &
Sort stringsstrcmp to compare, strcpy to swap
char *p[]Saves memory, literals read-only

Frequently asked questions

How do you store multiple strings in C?
Use a 2D char array like char names[10][20], or an array of char pointers.
How do you sort names alphabetically in C?
Bubble sort with strcmp(...) > 0 to compare and strcpy (three times) to swap.
What is the difference between char a[3][10] and char *p[3]?
a has fixed, editable rows; p holds pointers to strings, saving memory but literals can't be changed.

Remember

  • Array of strings = register notebook: one name per line.
  • Strings never use = or ==: use strcpy and strcmp.

Next Topic

Next lesson: Character Functions in C (ctype.h). You will test and convert single characters with ctype.h functions like isalpha and toupper.