我可以在C中创建一个Char指针数组吗?

我是C的新手,C中的事情与我学到的任何其他语言不同。 在我的作业中,我想创建一个指向一个字符数组的字符数组,但不是创建一个多维字符数组,我想我会有更多的控制并创建字符数组,并将每个字符放入索引中。原始字符数组:

char keywords[10]; keywords[0] = "float"; 

上面的例子是澄清一个简单的案例。 但我的问题是由于我一直在做的研究,我对某些事感到困惑。 通常这可以在其他语言中使用,但在C中它将是:

 char *keyword[10]; keywords[0] = "float"; 

但是当我想通过函数发送它时,为什么这是必要的:

 void function(char **keyword); //function prototype 

不只是传递数组指针就足够了吗?

看起来你对双星的困惑

 void function(char ** keyword); 

双星只是意味着这个函数要求你传递一个指向char的指针 。 此语法不包含有关您正在使用数组的事实的任何信息,或者char实际上是字符串中的许多char的第一个char。 作为程序员,您可以了解这个char **实际指向的数据结构类型。

例如,假设数组的开头存储在地址0x1000处。 函数的keyword参数的值应为0x1000。 如果你取消引用keyword ,你得到数组中的第一个条目,它是一个char * ,指向字符串“float”中的第一个字符。 如果取消引用char * ,则得到char“f”。

(人为的)代码如下所示:

 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] } 

上面的例子中有两个指针。 通过在解除引用之前向第一个指针添加偏移量,可以访问数组中的不同字符串。 通过在解除引用之前向第二个指针添加偏移量,可以访问字符串中的不同char。

 char *keyword[10]; 

keywordchar *的数组10。 在值上下文中,它转换为指向char *的指针。

这种转换是Chris Torek所说的“规则”的一部分

“如其他地方所述,C对数组和指针有一个非常重要的规则。这个规则 – 规则 – 说,在值上下文中,’T数组’类型的对象变成了’指向T的指针’的值’,指向该数组的第一个元素“

有关更多信息,请参见此处: http : //web.torek.net/torek/c/pa.html

C-FAQ在这个数组上还有一个指针转换条目:

问题6.3:那么C中的“指针和数组的等价”是什么意思?

http://c-faq.com/aryptr/aryptrequiv.html

如果你想

 void function(char **keyword); 

安迪,想想一个数组只是一个指针(到数组的开头),这就是你写的原因:

 void function(char **keyword); 

因为你已经为char指针创建了一个数组。

如果更容易理解尝试:

 void function(char *keyword[]); 

但是使用第一个是更标准的C标准,但是如果你使用C ++编译器并不重要。

这是答案。

 #include 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); } 

在C中,您无法将数组传递给函数。 而是将指针传递给数组的开头。 由于你有char*数组,函数将获得一个指向char*的指针,它是char**

如果需要,可以编写(在原型中) char *keyword[]而不是char **keyword 。 编译器会自动转换它。

另外,在C中你可以取消引用像数组这样的指针,所以你几乎没有“转换为指针”。

char *keywords[10]是一个字符指针数组。 因此keywords[0]keywords[1] ..等将具有不同字符数组的地址。

printf您可以使用%skeywords[0]来打印整个字符数组,其地址(即数组中第一个字节的地址)存储在keywords[0]

在传递给函数时,如果你给*keywords ,你指的是( keywords[0]存储的地址)值,它也是一个地址。 因此,要获取值而不是地址,您可以添加另一个* …希望澄清一点……

我假设你要分配你的第一个字符串:

 "float" 

到关键字[0]的第一个索引位置

 char keyword[0] = "float"; 

这是数组的第一个索引位置:

 char keyword[10]; 

如果是前一种情况,那么从某种意义上说,您实际上是在创建一个包含数据结构的数据结构。 任何类型的数组都是C中该类型的“最小”数据结构。考虑到在您的示例中您正在创建一个字符数组,那么您实际上是在每个索引位置使用最小的数据类型(char = 1bit)。最小的内置数据结构(数组)。

话虽如此,如果在您的示例中,您正在尝试创建一个数组数组; 你的角色arrays

 /* Hold ten characters total */ char keyword[10]; 

被设计为容纳10个字符。 每个指数位置一个(您可能已经知道)。 因此,在声明了名为keyword的数组之后,您尝试使用另一个(第二个)字符数组初始化数组的第一个索引位置:

 /* I believe this is what you had stated */ char keywords[0] = "float"; 

第二个字符数组的索引大小为5个位置。

为了实现您期望的目标,您基本上将创建一个数组,该数组基本上模拟“保持”其他数据结构的数据结构的效果。

注意:如果您已经/计划尝试创建一个包含数据结构的数据结构。 AKA是一个三重嵌套数据结构,在这种情况下,我认为这将是一个矩阵,我不推荐!

尽管如此,矩阵结构将是关键字的第一个索引位置的forms,被分配整个关键字数组,其将包括存储在关键字数组的每个索引位置中的所有数据。 那么可能会有:keywords1,keywords2,… keywords9,

这将基本上模仿以下forms:

 char *keyword[10] = { char *keywords0[10] = {"float", etc, etc, etc.}; char *keywords1[10] = {"keyword1", "secondIndexOfThisArray", etc, etc, etc.}; and so }; 

因此,从右到左基本上,关键字数组是一个指针数组,指向指向字符数组的指针数组。

如果这就是您所代表的,那么最好定义结构/记录的自定义数据类型,并且在该自定义结构中,您需要定义从属或子级别的结构。 您也可以预先声明它们然后初始化它们。

例如

 typedef *nestedDataStructures { struct keyWords[]; struct keyWords1[]; struct keyWords2[]; ... and so on. }; nestedDataStructures 

我不会在一个自定义结构中添加十个结构,而是分解为3或4(多少结构和使用)并创建一个模块,以便在操作数据集时产生对称的抽象层。

尽管如此,你不能创建角色数组并且可能以你所做的方式分配其他角色数组(或者你知道也许你可以),但是想要模拟包含数组的数组的方式是创建前面的字符指针数组,X个索引位置然后初始化然后使用在原始声明的初始化中声明的字符串forms的字符数组。

因此,基本上您可以预先声明整个数组,然后在程序设计中,取消引用每个索引位置,使用赋值或打印/写入索引位置。

比如你可以像这样做:

 /* Example of the program and declaration with out a function */ #include  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 :-) 

或类似的东西:

 /* Example of program: function passed arguments * of a pointer to the array of pointers */ #include  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; } } 

每个索引位置的赋值,因为它作为参数传递:

 /* * This program compiles, runs and outputs properly * Example of a program with a function of * arguments pnt2pnter */ #include  #include  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; } 

或者通过引用传递,循环计数变量的增量:

 /* * This program compiles, runs, and outputs all of the character * arrays. * */ #include  #include  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); } 

请记住,如果您声明指针数组(char * array [10];),并且每个指针指向一个字符数组,就会出现这种情况。