我假设你正在分配你的第一个字符串:
"float"
到关键字[0]的第一个索引位置
char keyword[0] = "float";
这是数组的第一个索引位置:
char keyword[10];
如果是前面的情况,那么从某种意义上说,您本质上是在创建一个包含数据结构的数据结构。任何类型的数组都是 C 中该类型的“最小”数据结构。考虑到在您的示例中您正在创建一个字符数组,那么您实际上是在每个索引位置使用最小的数据类型(char=1bit)最小的内置数据结构(数组)。
话虽如此,如果在您的示例中,您正在尝试创建一个数组数组;你的字符数组
/* Hold ten characters total */
char keyword[10];
旨在容纳 10 个字符。每个索引位置一个(您可能已经知道)。因此,在声明数组 titled 关键字后,您尝试用另一个(第二个)字符数组初始化数组的第一个索引位置:
/* I believe this is what you had stated */
char keywords[0] = "float";
第二个字符数组的索引大小为 5 个位置。
为了实现您想要的目标,您实际上将创建一个数组,该数组基本上模拟“保存”其他数据结构的数据结构的效果。
注意:如果您有/有计划尝试创建一个包含数据结构的数据结构。又名一个三重嵌套的数据结构,在这种情况下,我认为这将是一个矩阵,我不建议这样做!
尽管如此,矩阵结构将是关键字的第一个索引位置的形式,被分配了整个关键字数组,其中包括存储在关键字数组的每个索引位置的所有数据。然后可能会有类似:keywords1,keywords2,...keywords9,
本质上会模仿以下形式:
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个索引位置,然后初始化,然后使用在原始声明的初始化中声明的字符串形式的字符数组。
所以基本上你可以预先声明你的整个数组,然后在你的程序设计中,或者取消引用每个索引位置,使用赋值,或者打印/写入索引位置。
例如,您总是可以这样做:
/* 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 :-)
或者是这样的:
/* 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;
}
}
作为参数传递的每个索引位置的分配:
/*
* 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;
}
或者通过引用传递,增加循环计数变量:
/*
* 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);
}
请记住,如果您声明了指针数组 (char *array[10];),并且每个指针都指向一个字符数组,就会出现这种情况。