I am new to C, and things are different in C than in any other language I've learned. In my homework I want to create an array of chars which point to an array of chars, but rather than make a multidimensional char array, I figure I'd have more control and create char arrays and put each individual one into the indexes of the original char array:
我对C很陌生,C语言和我学过的其他语言都不一样。在我的家庭作业中,我想创建一个指向一个chars数组的chars数组,但不是创建一个多维的char数组,我想我有更多的控制权,创建char数组,并将每个单独的char数组放入原始char数组的索引中:
char keywords[10];
keywords[0] = "float";
The above example is to clarify and a simple case. But my question is due to the research I've been doing, and I am confused about something. Normally this would work in other languages, but in C it would be:
上面的示例是澄清和简单的情况。但我的问题是由于我所做的研究,我对一些事情感到困惑。通常这在其他语言中也适用,但在C语言中则是:
char *keyword[10];
keywords[0] = "float";
But when I want to send it through a function, why is this necessary:
但是当我想通过函数发送它时,为什么必须这样:
void function(char **keyword); //function prototype
Wouldn't just passing the array pointer be enough?
仅仅传递数组指针就足够了吗?
7 个解决方案
#1
10
It looks like you're confused by the double stars in
看起来你被里面的双星搞糊涂了
void function(char ** keyword);
The double stars just means that this function expects you to pass a pointer to a pointer to a char. This syntax doesn't include any information about the fact that you are using an array, or that the char is actually first char of many in a string. It's up to you as the programmer to know what kind of data structure this char **
actually points to.
双星号的意思是这个函数要求你传递一个指向字符的指针的指针。该语法不包含任何关于您正在使用数组的信息,也不包含字符实际上是字符串中许多字符的第一个字符。作为程序员,您应该知道这个char **实际上指向什么类型的数据结构。
For example, let's suppose the beginning of your array is stored at address 0x1000. The keyword
argument to the function should have a value of 0x1000. If you dereference keyword
, you get the first entry in the array, which is a char *
that points to the first char in the string "float". If you dereference the char *
, you get the char "f".
例如,假设您的数组的开头存储在地址0x1000中。函数的关键字参数应该有0x1000的值。如果您去引用关键字,您将获得数组中的第一个条目,它是一个char *,指向字符串“float”中的第一个char。如果你取消了char *,就会得到char "f"。
The (contrived) code for that would look like:
这个(人为设计的)代码看起来是这样的:
void function(char **keyword)
{
char * first_string = *keyword; // *keyword is equivalent to keyword[0]
char first_char = *first_string; // *first_string is equivalent to first_string[0]
}
There were two pointers in the example above. By adding an offset to the first pointer before dereferencing it, you can access different strings in the array. By adding an offset to the second pointer before dereferencing it, you can access different char in the string.
上面的示例中有两个指针。在取消引用前向第一个指针添加偏移量,可以访问数组中的不同字符串。通过在取消引用之前向第二个指针添加一个偏移量,您可以在字符串中访问不同的char。
#2
2
char *keyword[10];
keyword
is an array 10 of char *
. In a value context, it converted to a pointer to a char *
.
关键字是char *的数组10。在值上下文中,它转换为指向char *的指针。
This conversion is a part of what Chris Torek calls "The Rule":
这种转变是克里斯·托雷克所说的“规则”的一部分:
"As noted elsewhere, C has a very important rule about arrays and pointers. This rule -- The Rule -- says that, in a value context, an object of type ‘array of T’ becomes a value of type ‘pointer to T’, pointing to the first element of that array"
正如其他地方提到的,C对于数组和指针有一个非常重要的规则。这个规则——规则——说,在一个值上下文中,类型“T”数组的对象会变成“指向T的指针”的值,指向该数组的第一个元素“
See here for more information: http://web.torek.net/torek/c/pa.html
更多信息请参见这里:http://web.torek.net/torek/c/pa.html
The C-FAQ also has an entry on this array to pointer conversion:
C-FAQ中也有关于指针转换的条目:
Question 6.3: So what is meant by the "equivalence of pointers and arrays'' in C?
问题6.3:那么C中“指针和数组的等价性”是什么意思?
http://c-faq.com/aryptr/aryptrequiv.html
http://c-faq.com/aryptr/aryptrequiv.html
#3
1
If you want to
如果你想
void function(char **keyword);
Andy, think about that an array is just a pointer(to the beginning of the array), that's why you write:
安迪,想想一个数组只是一个指针(指向数组的开头),这就是为什么你写:
void function(char **keyword);
Because you have create an array to char pointers.
因为你已经为char指针创建了一个数组。
If it's easier to understand try:
如果它更容易理解,试试:
void function(char *keyword[]);
But it's more C standard to use the first one, though if you use a C++ compiler won't really matter.
但是使用第一个标准更符合C标准,不过如果使用c++编译器就没什么关系了。
#4
1
Here is the answer.
这是答案。
#include<stdio.h>
int main(void)
{
char *CharPtr[3];
char a[4]="abc";
char b[4]="def";
char c[4]="ghi";
CharPtr[0]=a;
CharPtr[1]=b;
CharPtr[2]=c;
printf("\n content of CharPtr[0] =%s",CharPtr[0]);
printf("\n content of CharPtr[1] =%s",CharPtr[1]);
printf("\n content of CharPtr[2] =%s\n",CharPtr[2]);
printf(" \n content of char a[4]=%s",a);
printf(" \n content of char b[4]=%s",b);
printf(" \n content of char c[4]=%s\n",c);
}
#5
0
In C, you can't really pass array to a function. Instead, you pass a pointer to the beginning of the array. Since you have array of char*
, the function will get a pointer to char*
, which is char**
.
在C语言中,你不能把数组传递给函数。相反,将指针传递到数组的开头。由于您有char*数组,函数将获得指向char*的指针,即char**。
If you want, you can write (in the prototype) char *keyword[]
instead of char **keyword
. The compiler will automatically convert it.
如果需要,可以用char *关键字[]代替char **关键字(在原型中)。编译器会自动转换它。
Also, in C you can dereference pointers like arrays, so you loose almost nothing with that "converting to pointer".
另外,在C语言中,你可以引用数组之类的指针,这样你就可以在“转换为指针”的情况下释放几乎所有的东西。
#6
0
char *keywords[10]
is an array of character pointers. So keywords[0]
, keywords[1]
.. and so on will have the addresses to different character arrays.
char *keywords[10]是一个字符指针数组。[1][0]所以关键字,关键字. .等等会有不同字符数组的地址。
In printf
you can use %s
and keywords[0]
to print the entire character array whose address(i.e. address of the first byte in the array) is stored at keywords[0]
.
在printf中,您可以使用%s和关键字[0]来打印整个字符数组的地址(即。数组中第一个字节的地址)存储在关键字[0]上。
While passing to a function, if you give *keywords
, you are referring to the value at(address stored at keywords[0]
) which is again an address. So, to get the value instead of address, you can add another *
... Hope that clarifies a bit..
在传递给函数时,如果您提供*关键字,那么您是在引用值at(以关键字[0]存储的地址),它也是一个地址。因此,要获取值而不是地址,您可以添加另一个*…希望这能澄清一点。
#7
0
I am assuming that you are assigning your first string:
我假设你正在分配你的第一个字符串:
"float"
to the first index position of keyword[0]
到关键字[0]的第一个索引位置
char keyword[0] = "float";
which is the first index position of the array:
为数组的第一个索引位置:
char keyword[10];
If the previous is the case, then in a sense, you are essentially creating a data structure that holds a data structure. The array of any type is the 'smallest' data structure of that type in C. Considering that in your example you are creating a character array, then you are actually utilizing the smallest data type (char=1bit) at each index position of the smallest built in data structure (the array).
如果是前一种情况,那么在某种意义上,您实际上是在创建一个包含数据结构的数据结构。任何类型的数组都是c中该类型的“最小”数据结构。考虑到在您的示例中您正在创建一个字符数组,那么您实际上是在数据结构(数组)中最小的索引位置上使用最小的数据类型(char=1bit)。
With that said, if in your example, you are attempting to create an array of arrays; your character array
说到这里,如果在示例中,您正在尝试创建数组数组;你的字符数组
/* Hold ten characters total */
char keyword[10];
was designed to hold 10 characters. One at each index position (which you probably already know). So after declaring the array titled keyword, you then try to initialize the first index position of the array with another (the second) character array:
设计为可容纳10个字符。每个索引位置都有一个(您可能已经知道了)。因此,在声明了名为关键字的数组之后,您将尝试使用另一个(第二个)字符数组初始化数组的第一个索引位置:
/* I believe this is what you had stated */
char keywords[0] = "float";
With the second character array having an index of 5 positions in size.
第二个字符数组的索引大小为5个位置。
In order to achieve your desired goal, you would essentially be creating an array that basically emulates the effect of a data structure that 'holds' other data structures.
为了达到你想要的目标,你实际上是在创建一个数组,它基本上模拟了“保存”其他数据结构的数据结构的效果。
NOTE: If you had/have plans on trying to create a data structure that holds a data structure that holds a data structure. A.K.A. a triple nested data structure and in this case I think that would be a Matrix, WHICH I WOULDN'T RECOMMEND!
注意:如果您打算创建一个包含数据结构的数据结构的数据结构。也就是一个三重嵌套的数据结构在这种情况下,我认为这是一个矩阵,我不推荐它!
None the less, the matrix structure would be in the form of the first index position of keyword, being assigned the whole array of keywords, which would include all of the data stored in each index position of the keywords array. Then there would something probably like: keywords1, keywords2, ... keywords9,
然而,矩阵结构的形式是关键字的第一个索引位置,被分配到整个关键字数组,其中包括所有存储在关键字数组的索引位置的数据。然后可能会出现这样的情况:keywords1, keywords2,……keywords9,
which would essentially emulate the form of:
这基本上是模仿的形式:
char *keyword[10] = {
char *keywords0[10] = {"float", etc, etc, etc.};
char *keywords1[10] = {"keyword1", "secondIndexOfThisArray", etc, etc, etc.};
and so
};
So basically from right to left, the keyword array, is an array of pointers that points to array of pointers that points to character arrays.
从右到左,关键字数组,是一个指针数组指向指针数组指向字符数组。
If that is what you are representing you would be better defining a custom data type of struct/record, and with in that custom structure you would want to define a subordinate or child level of structures. You could also pre-declare them then initialize them.
如果这就是您要表示的,那么您最好定义一个struct/record的自定义数据类型,在这个自定义结构中,您希望定义一个从属结构或子结构级别。您还可以预先声明它们,然后初始化它们。
e.g.
如。
typedef *nestedDataStructures {
struct keyWords[];
struct keyWords1[];
struct keyWords2[];
... and so on.
}; nestedDataStructures
Instead of adding ten structs to one custom structure I would break down into 3 or 4 (how ever many structures and use) and create a module in order to yield symmetrical layers of abstraction as you manipulate your data set.
我不再向一个自定义结构中添加10个结构,而是将其分解为3或4个(到底有多少结构和使用),并创建一个模块,以便在操作数据集时产生对称的抽象层。
None the less, you can not create the character array and potentially assign the other character array in the fashion that you did (or who knows maybe you can), but the way you would want to emulate the array that holds arrays, is to create a character pointer array up front, of X number index positions and then initialize then use the character arrays in the form of a strings declared with in the initialization of the original declaration.
依然,你不能创建字符数组并可能分配的其他字符数组方式你(或谁知道也许你可以),但是你想模仿的方式保存数组,数组是创建一个字符指针数组,X的数字索引位置然后初始化使用字符数组的形式字符串声明与初始化的原始声明。
So basically you could declare your whole array upfront, then with in your program design, either dereference each index position, use assignment, or print/write the index position.
基本上你可以预先声明你的整个数组,然后在你的程序设计中,删除每个索引位置,使用赋值,或者打印/写索引位置。
Like for instance you could always do something like this:
比如你可以这样做:
/* Example of the program and declaration with out a function */
#include <stdio.h>
int main(){
/*
* A character pointer array that contains multiple
* character arrays.
*/
char *grewMe[2] = {"I want to ", "grow to be bigger"};
int w = 0;
for(; w < 2;) {
printf("%s", grewMe[w]);
++w;
}
printf(" :-)\n");
w = 0;
return 0;
}
// Output:
// I want to grow to be bigger :-)
Or something like this:
或者是这样的:
/* Example of program: function passed arguments
* of a pointer to the array of pointers
*/
#include <stdio.h>
void mygrowth(char *growMe[]);
int main(){
char *growMe[2] = {"I want to ", "grow to be bigger"};
mygrowth(growMe);
printf(" :-)\n");
return 0;
}
void mygrowth(char *growMe[])
{
int w = 0;
for (; w < 2;) {
printf("%s", growMe[w]);
++w;
}
}
The assignment of each index position as it's passed as an argument:
作为参数传递的每个索引位置的赋值:
/*
* This program compiles, runs and outputs properly
* Example of a program with a function of
* arguments pnt2pnter
*/
#include <stdio.h>
#include <stdlib.h>
void thoughtAsAFunction(char **iThink);
int main()
{
char *iThink[10] = {"I am trying to grow, but it's a hard task to ",
"accomplish. My father is short ",
"my mother is even shorter than him, ",
"what is the probability of me getting taller? ",
"Well both my grandfather's were Six ",
"Foot Five, and both my grandmother's ",
"were over 5 foot 8 inches tall! If my ",
"grandparent's genes point to my parents, and my ",
"parent's genes point to mine I might have a chance ",
"of being 6 foot. Do you know what I mean? "};
thoughtAsAFunction(iThink);
printf(":-)\n");
return 0;
}
void thoughtAsAFunction(char **iThink) {
int andy = 0;
for (; andy < 10;) {
char * pntThroughPnt = iThink[andy];
printf("%s", pntThroughPnt);
++andy;
}
andy = 0;
}
Or pass by reference, with an increment of the loop count variable:
或通过引用传递,循环计数变量增加:
/*
* This program compiles, runs, and outputs all of the character
* arrays.
*
*/
#include <stdio.h>
#include <stdlib.h>
void thoughtAsAFunction(char **iThink);
int main()
{
char *iThink[10] = {"I am trying to grow, but it's a hard task to ",
"accomplish. My father is short ",
"my mother is even shorter than him, ",
"what is the probability of me getting taller? ",
"Well both my grandfather's were Six ",
"Foot Five, and both my grandmother's ",
"were over 5 foot 8 inches tall! If my ",
"grandparent's genes point to my parents, and my ",
"parent's genes point to mine, then I might have a chance ",
"of being 6 foot. Do you know what I mean? "};
int andy = 0;
for (; andy < 10;) {
// pass by reference and increment.
thoughtAsAFunction(&iThink[andy]);
++andy;
}
printf(":-)\n");
andy = 0;
return 0;
}
void thoughtAsAFunction(char **iThink) {
char * pntThroughPnt = *iThink;
printf("%s", pntThroughPnt);
}
Keep in mind that this is the case if you declare the array of pointers (char *array[10];), and each pointer points to an array of characters.
请记住,如果您声明指针数组(char *array[10];),并且每个指针指向一个字符数组,那么就是这种情况。
#1
10
It looks like you're confused by the double stars in
看起来你被里面的双星搞糊涂了
void function(char ** keyword);
The double stars just means that this function expects you to pass a pointer to a pointer to a char. This syntax doesn't include any information about the fact that you are using an array, or that the char is actually first char of many in a string. It's up to you as the programmer to know what kind of data structure this char **
actually points to.
双星号的意思是这个函数要求你传递一个指向字符的指针的指针。该语法不包含任何关于您正在使用数组的信息,也不包含字符实际上是字符串中许多字符的第一个字符。作为程序员,您应该知道这个char **实际上指向什么类型的数据结构。
For example, let's suppose the beginning of your array is stored at address 0x1000. The keyword
argument to the function should have a value of 0x1000. If you dereference keyword
, you get the first entry in the array, which is a char *
that points to the first char in the string "float". If you dereference the char *
, you get the char "f".
例如,假设您的数组的开头存储在地址0x1000中。函数的关键字参数应该有0x1000的值。如果您去引用关键字,您将获得数组中的第一个条目,它是一个char *,指向字符串“float”中的第一个char。如果你取消了char *,就会得到char "f"。
The (contrived) code for that would look like:
这个(人为设计的)代码看起来是这样的:
void function(char **keyword)
{
char * first_string = *keyword; // *keyword is equivalent to keyword[0]
char first_char = *first_string; // *first_string is equivalent to first_string[0]
}
There were two pointers in the example above. By adding an offset to the first pointer before dereferencing it, you can access different strings in the array. By adding an offset to the second pointer before dereferencing it, you can access different char in the string.
上面的示例中有两个指针。在取消引用前向第一个指针添加偏移量,可以访问数组中的不同字符串。通过在取消引用之前向第二个指针添加一个偏移量,您可以在字符串中访问不同的char。
#2
2
char *keyword[10];
keyword
is an array 10 of char *
. In a value context, it converted to a pointer to a char *
.
关键字是char *的数组10。在值上下文中,它转换为指向char *的指针。
This conversion is a part of what Chris Torek calls "The Rule":
这种转变是克里斯·托雷克所说的“规则”的一部分:
"As noted elsewhere, C has a very important rule about arrays and pointers. This rule -- The Rule -- says that, in a value context, an object of type ‘array of T’ becomes a value of type ‘pointer to T’, pointing to the first element of that array"
正如其他地方提到的,C对于数组和指针有一个非常重要的规则。这个规则——规则——说,在一个值上下文中,类型“T”数组的对象会变成“指向T的指针”的值,指向该数组的第一个元素“
See here for more information: http://web.torek.net/torek/c/pa.html
更多信息请参见这里:http://web.torek.net/torek/c/pa.html
The C-FAQ also has an entry on this array to pointer conversion:
C-FAQ中也有关于指针转换的条目:
Question 6.3: So what is meant by the "equivalence of pointers and arrays'' in C?
问题6.3:那么C中“指针和数组的等价性”是什么意思?
http://c-faq.com/aryptr/aryptrequiv.html
http://c-faq.com/aryptr/aryptrequiv.html
#3
1
If you want to
如果你想
void function(char **keyword);
Andy, think about that an array is just a pointer(to the beginning of the array), that's why you write:
安迪,想想一个数组只是一个指针(指向数组的开头),这就是为什么你写:
void function(char **keyword);
Because you have create an array to char pointers.
因为你已经为char指针创建了一个数组。
If it's easier to understand try:
如果它更容易理解,试试:
void function(char *keyword[]);
But it's more C standard to use the first one, though if you use a C++ compiler won't really matter.
但是使用第一个标准更符合C标准,不过如果使用c++编译器就没什么关系了。
#4
1
Here is the answer.
这是答案。
#include<stdio.h>
int main(void)
{
char *CharPtr[3];
char a[4]="abc";
char b[4]="def";
char c[4]="ghi";
CharPtr[0]=a;
CharPtr[1]=b;
CharPtr[2]=c;
printf("\n content of CharPtr[0] =%s",CharPtr[0]);
printf("\n content of CharPtr[1] =%s",CharPtr[1]);
printf("\n content of CharPtr[2] =%s\n",CharPtr[2]);
printf(" \n content of char a[4]=%s",a);
printf(" \n content of char b[4]=%s",b);
printf(" \n content of char c[4]=%s\n",c);
}
#5
0
In C, you can't really pass array to a function. Instead, you pass a pointer to the beginning of the array. Since you have array of char*
, the function will get a pointer to char*
, which is char**
.
在C语言中,你不能把数组传递给函数。相反,将指针传递到数组的开头。由于您有char*数组,函数将获得指向char*的指针,即char**。
If you want, you can write (in the prototype) char *keyword[]
instead of char **keyword
. The compiler will automatically convert it.
如果需要,可以用char *关键字[]代替char **关键字(在原型中)。编译器会自动转换它。
Also, in C you can dereference pointers like arrays, so you loose almost nothing with that "converting to pointer".
另外,在C语言中,你可以引用数组之类的指针,这样你就可以在“转换为指针”的情况下释放几乎所有的东西。
#6
0
char *keywords[10]
is an array of character pointers. So keywords[0]
, keywords[1]
.. and so on will have the addresses to different character arrays.
char *keywords[10]是一个字符指针数组。[1][0]所以关键字,关键字. .等等会有不同字符数组的地址。
In printf
you can use %s
and keywords[0]
to print the entire character array whose address(i.e. address of the first byte in the array) is stored at keywords[0]
.
在printf中,您可以使用%s和关键字[0]来打印整个字符数组的地址(即。数组中第一个字节的地址)存储在关键字[0]上。
While passing to a function, if you give *keywords
, you are referring to the value at(address stored at keywords[0]
) which is again an address. So, to get the value instead of address, you can add another *
... Hope that clarifies a bit..
在传递给函数时,如果您提供*关键字,那么您是在引用值at(以关键字[0]存储的地址),它也是一个地址。因此,要获取值而不是地址,您可以添加另一个*…希望这能澄清一点。
#7
0
I am assuming that you are assigning your first string:
我假设你正在分配你的第一个字符串:
"float"
to the first index position of keyword[0]
到关键字[0]的第一个索引位置
char keyword[0] = "float";
which is the first index position of the array:
为数组的第一个索引位置:
char keyword[10];
If the previous is the case, then in a sense, you are essentially creating a data structure that holds a data structure. The array of any type is the 'smallest' data structure of that type in C. Considering that in your example you are creating a character array, then you are actually utilizing the smallest data type (char=1bit) at each index position of the smallest built in data structure (the array).
如果是前一种情况,那么在某种意义上,您实际上是在创建一个包含数据结构的数据结构。任何类型的数组都是c中该类型的“最小”数据结构。考虑到在您的示例中您正在创建一个字符数组,那么您实际上是在数据结构(数组)中最小的索引位置上使用最小的数据类型(char=1bit)。
With that said, if in your example, you are attempting to create an array of arrays; your character array
说到这里,如果在示例中,您正在尝试创建数组数组;你的字符数组
/* Hold ten characters total */
char keyword[10];
was designed to hold 10 characters. One at each index position (which you probably already know). So after declaring the array titled keyword, you then try to initialize the first index position of the array with another (the second) character array:
设计为可容纳10个字符。每个索引位置都有一个(您可能已经知道了)。因此,在声明了名为关键字的数组之后,您将尝试使用另一个(第二个)字符数组初始化数组的第一个索引位置:
/* I believe this is what you had stated */
char keywords[0] = "float";
With the second character array having an index of 5 positions in size.
第二个字符数组的索引大小为5个位置。
In order to achieve your desired goal, you would essentially be creating an array that basically emulates the effect of a data structure that 'holds' other data structures.
为了达到你想要的目标,你实际上是在创建一个数组,它基本上模拟了“保存”其他数据结构的数据结构的效果。
NOTE: If you had/have plans on trying to create a data structure that holds a data structure that holds a data structure. A.K.A. a triple nested data structure and in this case I think that would be a Matrix, WHICH I WOULDN'T RECOMMEND!
注意:如果您打算创建一个包含数据结构的数据结构的数据结构。也就是一个三重嵌套的数据结构在这种情况下,我认为这是一个矩阵,我不推荐它!
None the less, the matrix structure would be in the form of the first index position of keyword, being assigned the whole array of keywords, which would include all of the data stored in each index position of the keywords array. Then there would something probably like: keywords1, keywords2, ... keywords9,
然而,矩阵结构的形式是关键字的第一个索引位置,被分配到整个关键字数组,其中包括所有存储在关键字数组的索引位置的数据。然后可能会出现这样的情况:keywords1, keywords2,……keywords9,
which would essentially emulate the form of:
这基本上是模仿的形式:
char *keyword[10] = {
char *keywords0[10] = {"float", etc, etc, etc.};
char *keywords1[10] = {"keyword1", "secondIndexOfThisArray", etc, etc, etc.};
and so
};
So basically from right to left, the keyword array, is an array of pointers that points to array of pointers that points to character arrays.
从右到左,关键字数组,是一个指针数组指向指针数组指向字符数组。
If that is what you are representing you would be better defining a custom data type of struct/record, and with in that custom structure you would want to define a subordinate or child level of structures. You could also pre-declare them then initialize them.
如果这就是您要表示的,那么您最好定义一个struct/record的自定义数据类型,在这个自定义结构中,您希望定义一个从属结构或子结构级别。您还可以预先声明它们,然后初始化它们。
e.g.
如。
typedef *nestedDataStructures {
struct keyWords[];
struct keyWords1[];
struct keyWords2[];
... and so on.
}; nestedDataStructures
Instead of adding ten structs to one custom structure I would break down into 3 or 4 (how ever many structures and use) and create a module in order to yield symmetrical layers of abstraction as you manipulate your data set.
我不再向一个自定义结构中添加10个结构,而是将其分解为3或4个(到底有多少结构和使用),并创建一个模块,以便在操作数据集时产生对称的抽象层。
None the less, you can not create the character array and potentially assign the other character array in the fashion that you did (or who knows maybe you can), but the way you would want to emulate the array that holds arrays, is to create a character pointer array up front, of X number index positions and then initialize then use the character arrays in the form of a strings declared with in the initialization of the original declaration.
依然,你不能创建字符数组并可能分配的其他字符数组方式你(或谁知道也许你可以),但是你想模仿的方式保存数组,数组是创建一个字符指针数组,X的数字索引位置然后初始化使用字符数组的形式字符串声明与初始化的原始声明。
So basically you could declare your whole array upfront, then with in your program design, either dereference each index position, use assignment, or print/write the index position.
基本上你可以预先声明你的整个数组,然后在你的程序设计中,删除每个索引位置,使用赋值,或者打印/写索引位置。
Like for instance you could always do something like this:
比如你可以这样做:
/* Example of the program and declaration with out a function */
#include <stdio.h>
int main(){
/*
* A character pointer array that contains multiple
* character arrays.
*/
char *grewMe[2] = {"I want to ", "grow to be bigger"};
int w = 0;
for(; w < 2;) {
printf("%s", grewMe[w]);
++w;
}
printf(" :-)\n");
w = 0;
return 0;
}
// Output:
// I want to grow to be bigger :-)
Or something like this:
或者是这样的:
/* Example of program: function passed arguments
* of a pointer to the array of pointers
*/
#include <stdio.h>
void mygrowth(char *growMe[]);
int main(){
char *growMe[2] = {"I want to ", "grow to be bigger"};
mygrowth(growMe);
printf(" :-)\n");
return 0;
}
void mygrowth(char *growMe[])
{
int w = 0;
for (; w < 2;) {
printf("%s", growMe[w]);
++w;
}
}
The assignment of each index position as it's passed as an argument:
作为参数传递的每个索引位置的赋值:
/*
* This program compiles, runs and outputs properly
* Example of a program with a function of
* arguments pnt2pnter
*/
#include <stdio.h>
#include <stdlib.h>
void thoughtAsAFunction(char **iThink);
int main()
{
char *iThink[10] = {"I am trying to grow, but it's a hard task to ",
"accomplish. My father is short ",
"my mother is even shorter than him, ",
"what is the probability of me getting taller? ",
"Well both my grandfather's were Six ",
"Foot Five, and both my grandmother's ",
"were over 5 foot 8 inches tall! If my ",
"grandparent's genes point to my parents, and my ",
"parent's genes point to mine I might have a chance ",
"of being 6 foot. Do you know what I mean? "};
thoughtAsAFunction(iThink);
printf(":-)\n");
return 0;
}
void thoughtAsAFunction(char **iThink) {
int andy = 0;
for (; andy < 10;) {
char * pntThroughPnt = iThink[andy];
printf("%s", pntThroughPnt);
++andy;
}
andy = 0;
}
Or pass by reference, with an increment of the loop count variable:
或通过引用传递,循环计数变量增加:
/*
* This program compiles, runs, and outputs all of the character
* arrays.
*
*/
#include <stdio.h>
#include <stdlib.h>
void thoughtAsAFunction(char **iThink);
int main()
{
char *iThink[10] = {"I am trying to grow, but it's a hard task to ",
"accomplish. My father is short ",
"my mother is even shorter than him, ",
"what is the probability of me getting taller? ",
"Well both my grandfather's were Six ",
"Foot Five, and both my grandmother's ",
"were over 5 foot 8 inches tall! If my ",
"grandparent's genes point to my parents, and my ",
"parent's genes point to mine, then I might have a chance ",
"of being 6 foot. Do you know what I mean? "};
int andy = 0;
for (; andy < 10;) {
// pass by reference and increment.
thoughtAsAFunction(&iThink[andy]);
++andy;
}
printf(":-)\n");
andy = 0;
return 0;
}
void thoughtAsAFunction(char **iThink) {
char * pntThroughPnt = *iThink;
printf("%s", pntThroughPnt);
}
Keep in mind that this is the case if you declare the array of pointers (char *array[10];), and each pointer points to an array of characters.
请记住,如果您声明指针数组(char *array[10];),并且每个指针指向一个字符数组,那么就是这种情况。