我假設要分配的第一串:以關鍵字的第一個索引位置
"float"
[0]
char keyword[0] = "float";
其是陣列的第一索引的位置:
char keyword[10];
如果以前是這種情況,那麼從某種意義上說,您基本上是在創建一個數據結構擁有一個數據結構。任何類型的數組都是C中該類型的'最小'數據結構。考慮到在你的例子中你正在創建一個字符數組,那麼你實際上正在使用最小的數據類型(char = 1bit)最小的內置數據結構(數組)。
這樣說,如果在你的例子中,你正在嘗試創建一個數組數組;你的字符排列
/* Hold ten characters total */
char keyword[10];
被設計成可容納10個字符。每個索引位置一個(你可能已經知道)。所以聲明陣列標題關鍵字後,然後嘗試初始化所述陣列的所述第一索引位置與另一(第二個)的字符數組:
/* 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];),並且每個指針都指向一個字符數組,則是這種情況。
你認爲/期望函數原型應該是什麼? – 2012-02-19 19:43:05
你也可以通過char *關鍵字[],意思是完全一樣的東西 – Matthieu 2012-02-19 19:44:38
是的,你可以......做你想做的事。但真正的問題是你有什麼好的C編程書籍閱讀...... – 2012-02-19 19:44:42