Programming Tips - C/C++: Declare and list a constant array of strings

Date: 2021feb17 Update: 2026sep24 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> int main() { // 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("number %s\n", *p); } #if defined(_WIN32) || defined(_WIN64) // Windows-style const LPCSTR numbers[] = { "one", "two", "three", NULL }; for (const LPCSTR *p = numbers; *p; p++) { printf("%s\n", *p); } #endif // // -------------------------- // putchar('\n'); // Or without the NULL at the end: const char *count[] = { "one", "two", "three" }; // Iterate const int n = sizeof(count) / sizeof(count[0]); printf("There are %d items\n", n); for (int i = 0; i < n; i++) { const char *p = count[i]; printf("count %s\n", p); } }
Output:
number one number two number three There are 3 items count one count two count three