[英]2D array of pointers
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.现在我必须将 List1,..,List4 存储到另一个数组中,如下所述。
char **var_name[2][2] = { &List1, &List2, &List3, &List4};
What I should called to "var_name"?我应该叫什么“var_name”? & Is it right way? & 方法对吗?
Also If I want to print "Book" using XYZ, then how I can print it?另外,如果我想使用 XYZ 打印“书”,那么我该如何打印呢?
List1
is an array of two arrays of 10 const char
. List1
是 10 个const char
的两个 arrays 的数组。 So &List1
is a pointer to an array of two arrays of 10 char
.所以&List1
是一个指向两个 arrays 的 10 char
数组的指针。 If var_name
is an array of those, it is an array of pointers to an array of two arrays of 10 const char
.如果var_name
是这些数组,它是一个指针数组,指向由 10 个const char
的两个 arrays 组成的数组。 You can build a declaration for that piece by piece:您可以逐个构建该声明:
var_name
is an array…: var_name[]
. var_name
是一个数组……: var_name[]
。*var_name[]
. ……指针: *var_name[]
。(*var_name[])[2]
. …到两个数组…: (*var_name[])[2]
。(*var_name[])[2][10]
. …10 个数组…: (*var_name[])[2][10]
。const char
: const char (*var_name[])[2][10]
. … const char
: const char (*var_name[])[2][10]
。Then you can define and initialize it:然后你可以定义和初始化它:
char (*var_name[])[2][10] = { &List1, &List2, &List3, &List4};
"Book"
is in element 1 of List3
, which is in element 2 of var_name
, so you can refer to it with (*var_name[2])[1]
. "Book"
在List3
的元素 1 中,在var_name
的元素 2 中,因此您可以使用(*var_name[2])[1]
来引用它。
Note that this requires the *
, because var_name[i]
is a pointer.请注意,这需要*
,因为var_name[i]
是一个指针。 This follows the sample code you gave where the array is initialized with &List1
.这遵循您使用&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.) (memory中的地址相同,但类型不同。)
Suppopse we want to eliminate this unnecessary pointer level and initialize var_name
with { List1, List2, List3, List4 }
.假设我们想消除这个不必要的指针级别并用{ List1, List2, List3, List4 }
初始化var_name
。 As usual in C, those arrays will be automatically converted to pointers to their first elements.像往常一样在 C 中,那些 arrays 将自动转换为指向其第一个元素的指针。 The first element of each of those arrays is an array of 10 const char
.每个 arrays 的第一个元素是 10 个const char
的数组。 So we will be initializing var_name
with pointers to arrays of 10 const char
.因此,我们将使用指向 10 个const char
的 arrays 的指针来初始化var_name
。
Thus, var_name
will be an array of pointers to arrays of 10 const char
.因此, var_name
将是一个指向 10 个const char
的 arrays 的指针数组。 Again, we can build the declaration piece by piece:同样,我们可以逐个构建声明:
var_name
is an array…: var_name[]
. var_name
是一个数组……: var_name[]
。*var_name[]
. ……指针……: *var_name[]
。(*var_name[])[10]
. ... 到 10 的 arrays ...: (*var_name[])[10]
。const char
…: const char (*var_name[])[10]
. … const char
…: const char (*var_name[])[10]
。Then the definition and initialization is:那么定义和初始化就是:
const char (*var_name[])[10] = { List1, List2, List3, List4 };
Now the elements of List3
are pointed to by var_name[2]
, so we can refer to "Book"
with var_name[2][1]
.现在List3
的元素由var_name[2]
指向,所以我们可以用var_name[2][1]
来引用"Book"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.