Date: 2021feb17
Update: 2025sep2
Language: C/C++
Keywords: win32, iterate
Q. C/C++: Declare and list a constant array of strings
A. Here is some short idiomatic C/C++ code to do that:
#include <stdio.h>
// Declare the array using this syntax - make sure you put a NULL at the end
const char *numbers[] = { "one", "two", "three", NULL };
// Now, we can easily iterate through the array.
// We use a `char **` pointer because its pointing to an array of strings
// (each string is an array of chars).
// The end test - simply `*p` means continue until its NULL
for (const char **p = numbers; *p; p++) {
printf("%s\n", *p);
}
// Windows-style
const LPCSTR numbers[] = { "one", "two", "three", NULL };
for (const LPCSTR *p = numbers; *p; p++) {
printf("%s\n", *p);
}