🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Array of Pointers to Strings
Learn how an array of pointers to strings works in C, why it is the favourite table type in embedded C programming, and how to build it step by step in this free C programming course.
An array of pointers to strings is one of the most useful tools you will meet in a free C tutorial for embedded systems. Instead of copying characters into a big grid, you keep a small table of addresses, and every address leads to a string stored somewhere else. Firmware uses this trick for menu names, state names, error messages, AT command lists and register names. In this lecture of our free embedded C course you will build one from scratch, print what it really holds, and understand every byte involved.
Contents of this lecture
What You Will Learn
- What each slot of an array of pointers to strings contains
- How to declare the array and assign strings after the declaration
- Why assignment is legal here but illegal for a two-dimensional char array
- How to print string contents, string addresses and slot addresses
- Why the table costs only one pointer per entry
- Why we declare the elements as
const char *in modern C
Prerequisites
Before starting this free c programming course lecture, you should be comfortable with pointers and pointer arithmetic from Chapter 8, with one-dimensional arrays from Chapter 7, and with string basics from lectures cp9_1 to cp9_23. If the idea of a string as a null terminated char array is still fuzzy, revise cp9_1 first.
What an Array of Pointers to Strings Really Stores
A string in C is just a run of characters ending with '\0'. A string literal such as "IDLE" is placed by the compiler into read-only memory, and the expression "IDLE" evaluates to the address of its first character. That is the key. When you write an array whose element type is const char *, every element is only an address. The characters are never inside the array.
Compare this with a two-dimensional char array. There the characters live inside the array itself, in fixed-size rows. With a pointer array the table is tiny and uniform: one pointer per entry, regardless of how long each string is. On a 64-bit Linux PC a pointer is 8 bytes. On a Cortex-M microcontroller it is 4 bytes. The idea is identical.
Remember
The array holds addresses. The strings sit in another place. Changing a slot changes where it points, it never touches a character.
Declaring and Assigning the Pointer Array
The declaration reserves space for the pointers only:
const char *state[4];
Read it right to left: state is an array of 4 elements, each element is a pointer to const char. After the declaration each slot holds garbage, so you must fill it before use. Because every slot is a plain pointer variable, an ordinary assignment works:
state[0] = "IDLE";
state[1] = "SCANNING";
This is a big difference from a two-dimensional array. With char grid[4][12] the statement grid[0] = "IDLE"; is rejected by the compiler, because grid[0] is an array and arrays cannot be assigned. A pointer slot can accept the address of any string, so the assignment is fine. You can also initialise everything in one go:
const char *state[] = { "IDLE", "SCANNING", "CONNECTED", "FAULT" };
With empty brackets the compiler counts the initialisers for you, which is the safest way to avoid a size mismatch.
Why const char * and not char *
String literals must never be modified. Older C code often wrote char *p = "text"; and compiled, but writing through that pointer is undefined behaviour and usually crashes on a microcontroller with the literal stored in flash. Writing const char * makes the compiler stop you. Use it for every pointer that refers to a literal.
Memory Picture of the Pointer Array
The next program prints three things for every slot: where the slot itself sits, which address it holds, and the string it leads to. It also prints the size of the table. Save it as ptr_table.c:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *state[4];
state[0] = "IDLE";
state[1] = "SCANNING";
state[2] = "CONNECTED";
state[3] = "FAULT";
for (int i = 0; i < 4; i++) {
printf("slot %d at %p holds %p -> \"%s\" (%zu chars)\n",
i, (const void *)&state[i], (const void *)state[i],
state[i], strlen(state[i]));
}
printf("\nsizeof(state) = %zu bytes\n", sizeof state);
printf("sizeof(state[0]) = %zu bytes\n", sizeof state[0]);
state[1] = state[3]; /* repoint a slot, no characters copied */
printf("after repoint: slot 1 -> \"%s\"\n", state[1]);
return 0;
}
Build and Run the Demo
gcc -std=c11 -Wall -Wextra -pedantic -o ptr_table ptr_table.c
./ptr_table
Expected output (the hexadecimal addresses will be different on your machine, but the pattern stays the same):
slot 0 at 0x7fff855fc920 holds 0x56048f314008 -> "IDLE" (4 chars)
slot 1 at 0x7fff855fc928 holds 0x56048f31400d -> "SCANNING" (8 chars)
slot 2 at 0x7fff855fc930 holds 0x56048f314016 -> "CONNECTED" (9 chars)
slot 3 at 0x7fff855fc938 holds 0x56048f314020 -> "FAULT" (5 chars)
sizeof(state) = 32 bytes
sizeof(state[0]) = 8 bytes
after repoint: slot 1 -> "FAULT"
How to read this output
| Observation | What it tells you |
|---|---|
| Slot addresses grow by 8 (…920, …928, …930, …938) | Slots are packed back to back, each one a pointer of 8 bytes |
| Held addresses grow by the string length plus one | The compiler stored the literals side by side, each with its own '\0'. This layout is typical but not guaranteed by the language |
| Slot addresses look very different from held addresses | The table is on the stack, the literals are in read-only data |
sizeof(state) is 32 | 4 slots times 8 bytes, independent of how long the strings are |
Repointing a Slot
The last statement of the demo, state[1] = state[3];, copies one address into another slot. No character moves. Now slots 1 and 3 point to the very same "FAULT" string. This is exactly why swapping and sorting pointer arrays is cheap: you only exchange small addresses. We will exploit that in the sorting lecture of this series.
Embedded C Programming Use Cases
| Where | What the pointer array holds |
|---|---|
| Debug UART | Names for each state of a state machine, printed when it changes |
| Command shell | List of command words matched with strcmp |
| Display menus | One label per menu line on an LCD |
| Error reporting | Message text indexed by an error code |
| Modem drivers | AT command strings sent in sequence |
Common Mistakes
Mistake 1: using a slot before assigning it. A slot of an unfilled local array contains garbage. Printing it with %s crashes or prints junk.
Mistake 2: modifying a literal. Doing state[0][0] = 'i'; writes into read-only memory. Keep the element type const char * so the compiler catches this.
Mistake 3: printing the pointer with %s instead of %p. %s prints the characters, %p prints the address. Cast to (const void *) for %p.
Mistake 4: hard-coding the count. Use sizeof state / sizeof state[0] so adding an entry cannot break your loop.
Best Practices
- Declare literal tables as
static const char *const name[]so both the pointers and the text are read-only and can live in flash - Let the compiler count the entries with empty brackets
- Derive the element count with
sizeof, never a typed number - Print addresses with
%ponly for learning, never in production logs
Summary and Key Takeaways
- An array of pointers to strings stores addresses, not characters
- Assignment to a slot is legal because each slot is a pointer variable
- The table size is the number of entries times the pointer size
- Use
const char *for anything that points to a literal - Repointing is cheap and never copies characters
You now understand the structure behind every string table in embedded C programming. In the next lecture we compare this layout with the two-dimensional char array and see exactly which operations are valid on each.
Frequently Asked Questions
What is an array of pointers to strings in C?
It is an array whose elements are pointers to char. Each element holds the address of the first character of a string stored elsewhere.
Why can I assign a string to a pointer slot but not to a 2-D array row?
A pointer slot is a variable that can hold any address. A row of a 2-D array is an array, and C does not allow assigning to an array.
Does the pointer array copy the string?
No. It stores only the address. The text stays in its original location.
How much memory does the table use?
One pointer per entry, for example 4 bytes on a 32-bit microcontroller, plus the literal bytes stored separately.
Should I use char * or const char *?
Use const char * for literals. Modifying a literal is undefined behaviour.
Is this useful in a free embedded C course project?
Yes. State names, command tables and error text tables are in almost every firmware project.
Continue the Free C Programming Course
Keep going with the free c tutorial for embedded systems. Next we compare pointer arrays with 2-D char arrays.

2 Comments