I'm working an application which required the below thinngs.
const char List1[2][10] = {"Stack","Overflow"};
const char List2[2][10] = {"Goo","Gle"};
const char List3[2][10] = {"Face","Book"};
const char List4[2][10] = {"Pizza","Hutt"};
Now I've to store List1,..,List4 into another array as explained below.
char **var_name[2][2] = { &List1, &List2, &List3, &List4};
What I should called to "var_name"? & Is it right way?
Also If I want to print "Book" using XYZ, then how I can print it?
List1is an array of two arrays of 10const char. So&List1is a pointer to an array of two arrays of 10char. Ifvar_nameis an array of those, it is an array of pointers to an array of two arrays of 10const char. You can build a declaration for that piece by piece:var_nameis an array…:var_name[].*var_name[].(*var_name[])[2].(*var_name[])[2][10].const char:const char (*var_name[])[2][10].Then you can define and initialize it:
"Book"is in element 1 ofList3, which is in element 2 ofvar_name, so you can refer to it with(*var_name[2])[1].Note that this requires the
*, becausevar_name[i]is a pointer. This follows the sample code you gave where the array is initialized with&List1. It is more common to use a pointer to the first element of an array instead of a pointer to the array. (The addresses in memory are the same, but the types are different.)Suppopse we want to eliminate this unnecessary pointer level and initialize
var_namewith{ List1, List2, List3, List4 }. As usual in C, those arrays will be automatically converted to pointers to their first elements. The first element of each of those arrays is an array of 10const char. So we will be initializingvar_namewith pointers to arrays of 10const char.Thus,
var_namewill be an array of pointers to arrays of 10const char. Again, we can build the declaration piece by piece:var_nameis an array…:var_name[].*var_name[].(*var_name[])[10].const char…:const char (*var_name[])[10].Then the definition and initialization is:
Now the elements of
List3are pointed to byvar_name[2], so we can refer to"Book"withvar_name[2][1].