Advanced Pointers
String Table
A table of string pointers lets one index select a message.
String Table
string_table.c
#include <stdio.h>
int main(void) {
int index = ;
const char *names[3] = {"red", "green", "blue"};
const char *selected = names[index];
printf("name=%s\n", selected);
return 0;
}
#include <stdio.h>
int main(void) {
int index = ;
const char *names[3] = {"red", "green", "blue"};
const char *selected = names[index];
printf("name=%s\n", selected);
return 0;
}
#include <stdio.h>
int main(void) {
int index = ;
const char *names[3] = {"red", "green", "blue"};
const char *selected = names[index];
printf("name=%s\n", selected);
return 0;
}
pointer table
An array of `const char *` values stores addresses of string literals.
selected string
Changing an index chooses a different string without copying text.