You need to specify the size for the message array element in the structure definition:
#define N ... // maximum number of elements in message array typedef struct { char *action; char *message[N]; } lookuptab; lookuptab tab[] = { {"aa", {"bb", "cc"}}, {"dd", {"ee", "ff"}}, ... };
In this case, N must be at least 2.
If you want each instance of the lookuptab structure lookuptab have a different number of elements in the message array, you will have to allocate each message array separately, that is, you cannot use a static initializer:
typedef struct { char *action; char **messages; } lookuptab; lookuptab *newEntry(const char *action, size_t numMessages, ...) { lookuptab *entry = malloc(sizeof *entry); if (entry) { entry->action = malloc(strlen(action) + 1); if (entry->action) strcpy(entry->action, action); if (numMessages > 0) { entry->messages = malloc(sizeof *entry->messages * numMessages); if (entry->messages) { size_t i; va_list ap; va_start(ap, numMessages); for (i = 0; i < numMessages; i++) { char *nextMessage = va_arg(ap, char *); entry->messages[i] = malloc(strlen(nextMessage) + 1); if (entry->messages[i]) strcpy(entry->messages[i], nextMessage); } } } } return entry; } int main(void) { lookuptab *tab[ENTRIES];
Instead of explicitly passing the number of messages, you can use the watch:
tab[0] = newEntry("AA", "BB", "CC", NULL);
but you will have to either iterate over all the arguments twice (first, to get a number to select the messages array, then copy each message), or you will need a realloc() array for each message, for example:
size_t numMessages = 0; ... char *nextMessage while ((nextMessage = va_arg(ap, char *)) != NULL) { char **tmp = realloc(entry->messages, sizeof *entry->messages, numMessages+1); if (tmp) { entry->messages = tmp; entry->messages[numMessages] = malloc(strlen(nextMessage) + 1); strcpy(entry->messages[numMessages], nextMessage); numMessages++; } }