C array of string arrays

In C, I need to statically pre-allocate an array of numbers, each of which is associated with another array of strings. Will the following code be as follows:

struct number_and_strings { int nnn; char **sss; } static struct number_and_strings my_list[] = { {12, {"apple","banana","peach","apricot","orange",NULL}}, {34, {"tomato","cucumber",NULL}}, {5, {"bread","butter","cheese",NULL}}, {79, {"water",NULL}} } 
+4
source share
1 answer

sss - pointer to a pointer. Thus, an array of pointers cannot be directly bound to it. You can assign as follows using compound literals (which is a function of C99):

 static struct number_and_strings my_list[] = { {12, (char*[]){"apple","banana","peach","apricot","orange",NULL}}, {34, (char*[]){"tomato","cucumber",NULL}}, {5, (char*[]){"bread","butter","cheese",NULL}}, {79, (char*[]){"water",NULL}} }; 
+5
source

All Articles