What are Array of Strings in C- Embedded C Programming Course Online

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Array of Strings in C

Lecture from our free C programming course: store many strings in one two-dimensional char array, understand its memory layout, and read single strings or single characters with the right subscript.

An array of strings in C is the structure you reach for whenever a program needs a table of names: peripheral names, menu captions, error messages, command words. This lecture belongs to our free C programming course and shows how a two-dimensional char array holds several strings, how the compiler lays them out byte by byte, and how arr[i] and arr[i][j] reach different things. If you are following this free embedded C course to prepare for firmware work, this is one of the most reused patterns you will meet.

What You Will Learn

  • How to declare and initialize an array of strings in C with a 2-D char array
  • What the first and the second subscript of the array really mean
  • How the rows are placed in memory, using byte offsets
  • The difference between names[i] (a whole string) and names[i][j] (one character)
  • Why the terminating NUL byte must always have room
  • How this pattern shows up in embedded C programming

Prerequisites

You should already be comfortable with one-dimensional char arrays used as strings, the NUL terminator, and the idea that a two-dimensional array is an array whose elements are arrays. If any of that feels shaky, revisit the earlier lectures of this free C tutorial first. All examples compile with gcc -std=c17 -Wall -Wextra on any Linux machine or inside WSL.

Why an Array of Strings in C?

A single string is a one-dimensional array of characters. Real programs rarely need just one. A UART driver prints a status name, a command shell compares user input against a list of words, a display routine picks a caption by index. Writing a separate variable for every string would make loops impossible. Packing them into one structure lets you walk them with a simple for loop and an index.

In embedded C programming this idea is everywhere: a lookup table of register names for a debug dump, the text for each error code, labels for a small LCD menu. The two-dimensional character array is the most direct way to build such a table, and it is the simplest model to learn before moving to pointer-based tables later in this chapter.

Declaring a 2-D Character Array of Strings

The general form is:

char name[COUNT][LEN];

Read it as: COUNT strings, each one allowed to occupy at most LEN bytes. Those LEN bytes include the NUL terminator, so the longest text you can store is LEN - 1 characters.

PartMeaningExample value
First subscriptHow many strings the table holds5
Second subscriptBytes reserved for each string, NUL included8
Total sizeFirst subscript multiplied by second subscript40 bytes
Element typeOne char, always one byte1 byte

Initializing with string literals

The compact and readable way is to give one string literal per row:

#define COUNT 5
#define LEN   8

char periph[COUNT][LEN] = { "UART", "I2C", "SPI", "GPIO", "TIMER" };

Each literal initializes one row. The compiler copies the characters, adds the NUL, and fills every unused byte of the row with zero. So the row for "SPI" holds S, P, I, then five zero bytes.

The long form with single characters

The same table can be written character by character, which is exactly what the compact form means to the compiler:

char periph[COUNT][LEN] = {
    {'U','A','R','T','\0'},
    {'I','2','C','\0'},
    {'S','P','I','\0'},
    {'G','P','I','O','\0'},
    {'T','I','M','E','R','\0'}
};

Any element you do not list is set to zero, so the long form and the literal form produce identical memory. Nobody writes the long form in practice, but seeing it makes the layout obvious.

Memory Layout of the Array of Strings

C stores a two-dimensional array in row-major order: the whole of row 0, then the whole of row 1, and so on, with no gaps between rows. Row i therefore starts exactly i * LEN bytes after the start of the array. The diagram below shows the 40 bytes of our periph table, one row per string.

Row-major layout of char periph[5][8]
RowStart offset+0+1+2+3+4+5+6+7
periph[0]0UART\0\0\0\0
periph[1]8I2C\0\0\0\0\0
periph[2]16SPI\0\0\0\0\0
periph[3]24GPIO\0\0\0\0
periph[4]32TIMER\0\0\0

Notice that every row begins at a multiple of 8. That regularity is the reason the compiler can find periph[i] with a single multiplication and an addition, and the reason there are no pointers stored anywhere in this structure. The table is just 40 plain bytes.

Accessing Strings and Characters

With the layout in mind the two kinds of subscript become easy to separate.

ExpressionWhat it denotesType seen by your code
periphThe whole table, address of the first rowarray of 5 arrays of 8 char
periph[i]The i-th string, address of its first characterarray of 8 char, which decays to char *
periph[i][j]The j-th character of the i-th stringchar

So one subscript selects a string, and a second subscript selects a character inside that string. Because periph[i] is already a string, you can hand it straight to printf("%s", ...), strlen or strcmp.

A complete example with build steps

Save the following as strtable.c. It prints the sizes, then each string with its byte offset from the start of the table, then two single characters.

#include <stdio.h>

#define COUNT 5
#define LEN   8

int main(void)
{
    char periph[COUNT][LEN] = { "UART", "I2C", "SPI", "GPIO", "TIMER" };

    printf("sizeof(periph)    = %zu\n", sizeof periph);
    printf("sizeof(periph[0]) = %zu\n", sizeof periph[0]);
    printf("sizeof(periph[0][0]) = %zu\n", sizeof periph[0][0]);

    for (int i = 0; i < COUNT; i++) {
        printf("periph[%d] = %-6s offset = %td\n",
               i, periph[i], periph[i] - periph[0]);
    }
    printf("periph[3][1] = %c\n", periph[3][1]);
    printf("periph[4][4] = %c\n", periph[4][4]);
    return 0;
}

Build and run:

gcc -std=c17 -Wall -Wextra -o strtable strtable.c
./strtable

Expected output:

sizeof(periph)    = 40
sizeof(periph[0]) = 8
sizeof(periph[0][0]) = 1
periph[0] = UART   offset = 0
periph[1] = I2C    offset = 8
periph[2] = SPI    offset = 16
periph[3] = GPIO   offset = 24
periph[4] = TIMER  offset = 32
periph[3][1] = P
periph[4][4] = R

The offsets 0, 8, 16, 24 and 32 match the diagram exactly. The expression periph[i] - periph[0] counts bytes because both operands are char * values. The two single characters come from row 3 (GPIO, second character P) and row 4 (TIMER, fifth character R).

Real-World Uses in Embedded Systems

  • Error text tables: index by an error code and print the matching message on a debug UART.
  • Menu captions: a small OLED or LCD menu keeps its labels in one table and loops to draw them.
  • Command words: a serial console compares typed words against a fixed list.
  • Device name registry: a bootloader stores a short name per attached sensor for logging.
Common mistake: making LEN exactly as long as the longest text. A row declared as char t[1][5] = {"TIMER"}; is legal C, but all five bytes are letters and no NUL fits. Passing that row to printf("%s") reads past the end. Always size LEN as the longest string length plus one.

Best Practices for an Array of Strings in C

  1. Name the two dimensions with #define or enum constants instead of bare numbers, so loops and declarations stay in agreement.
  2. Compute the element count with sizeof table / sizeof table[0] when the table is initialized without a first subscript.
  3. Use sizeof of a row, not a hand-typed number, when you bound a copy into that row.
  4. Choose LEN from the longest real entry, not from a guess; the next lecture shows why oversized rows cost memory.

Performance and security notes

Access is fast: finding row i costs one multiply and one add, and the data sits contiguously, which is friendly to caches on larger processors. The security angle is the fixed row size. Writing a longer string into a row overflows into the next row, silently corrupting a different string. Later lectures show safe copy patterns.

Summary and Key Takeaways

  • An array of strings in C is usually a two-dimensional char array: char name[COUNT][LEN].
  • Rows are contiguous, so row i starts at byte i * LEN.
  • name[i] is one whole string; name[i][j] is one character.
  • Unused bytes in a row are zero-filled when you initialize with literals.
  • Each row must have space for its NUL terminator.

Conclusion

You now know how a table of text is represented in a plain C array and how to read it safely. This is the foundation for the rest of the Strings chapter in this free C programming course. In the next lecture we measure how much of that 40-byte table is actually padding, which matters a great deal on microcontrollers with only a few kilobytes of RAM.

Frequently Asked Questions

What is an array of strings in C?

It is a collection of strings stored under one name, most often as a two-dimensional char array where each row is one NUL-terminated string.

What do the two subscripts in char names[5][10] mean?

The first is the number of strings, the second is the number of bytes reserved for each string including the NUL terminator.

Why does names[i] work with printf and %s?

Because a row is a char array holding a string, and in an expression it turns into a pointer to its first character, which is what %s expects.

How many bytes does char t[5][8] occupy?

Forty bytes, which is 5 multiplied by 8, no matter how short the stored strings are.

What happens if a string is longer than the row?

Initialization with a literal that is too long is a compile-time error or warning, while copying at run time can overflow into the next row if you do not bound the copy.

Is this pattern used in embedded C programming?

Yes. Error messages, menu labels and command lists in firmware are commonly kept in tables like this one.

Continue the Free C Programming Course

Keep building your embedded C skills with the next lecture in this free C tutorial series, completely free on EmbeddedPathashala.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →