Date: 2014apr8
Update: 2026sep29
Language: C/C++
Platform: agnostic
Q. C/C++: Efficiently loop thru a constant array of strings
A. Here is a nice way to do that:
const char *things[] = { "One", "Two", "Three", NULL }; // NULL terminated
for (const char **p = things; *p; p++) {
printf("thing=%s\n", *p); // Use star-p
}
This only works when you can define your own NULL-terminated array.
Full Example:
#include <stdio.h>
const char *things[] = { "One", "Two", "Three", NULL }; // NULL terminated
int main() {
for (const char **p = things; *p; p++) {
printf("thing=%s\n", *p); // Use star-p
}
}
Output:
thing=One
thing=Two
thing=Three