2011-03-02 75 views
11

我想在結構中有兩個數組,它們在開始時被初始化,但需要進一步編輯。我需要結構的三個實例,以便我可以索引到一個特定的結構並根據需要進行修改。可能嗎?C中的數組struct

這就是我想我可以做的,但我得到的錯誤:

struct potNumber{ 
    int array[20] = {[0 ... 19] = 10}; 
    char *theName[] = {"Half-and-Half", "Almond", "Rasberry", "Vanilla", …}; 
} aPot[3]; 

然後我訪問結構如下:

printf("some statement %s", aPot[0].array[0]); 
aPot[0].theName[3]; 
… 
+6

你會得到什麼錯誤? – 2011-03-02 16:38:59

+3

這真的是你的'struct'是如何定義的? – birryree 2011-03-02 16:46:57

回答

11

的結構本身沒有數據。您需要創建結構類型的對象和設置的對象...

struct potNumber { 
    int array[20]; 
    char *theName[42]; 
}; 

/* I like to separate the type definition from the object creation */ 
struct potNumber aPot[3]; 
/* with a C99 compiler you can use 'designated initializers' */ 
struct potNumber bPot = {{[7] = 7, [3] = -12}, {[4] = "four", [6] = "six"}}; 

for (i = 0; i < 20; i++) { 
    aPot[0].array[i] = i; 
} 
aPot[0].theName[0] = "Half-and-Half"; 
aPot[0].theName[1] = "Almond"; 
aPot[0].theName[2] = "Rasberry"; 
aPot[0].theName[3] = "Vanilla"; 
/* ... */ 

for (i = 0; i < 20; i++) { 
    aPot[2].array[i] = 42 + i; 
} 
aPot[2].theName[0] = "Half-and-Half"; 
aPot[2].theName[1] = "Almond"; 
aPot[2].theName[2] = "Rasberry"; 
aPot[2].theName[3] = "Vanilla"; 
/* ... */ 
+0

不要忘了你可以初始化結構;原諒糟糕的格式化:struct potNumber aPot = {{[0] ... [19] = 10},{「Half-and-Half」,「Almond」,「Rasberry」,「Vanilla」.......}}; – 2011-03-02 16:48:44

+0

@大衛:是的,除了'...',這是有效的。示例添加在我上面的代碼片段中。謝謝。 – pmg 2011-03-02 16:55:48

+0

@David:這些都是GCC風格的初始化(因爲它是Linux,我認爲是GCC)。我從來沒有見過'......'的東西,那甚至是有效的? – birryree 2011-03-02 16:56:04

9

在C結構數組元素必須有一個固定的大小,所以char *theNames[]無效。你也無法以這種方式初始化結構。在C數組中是靜態的,即不能動態改變它們的大小。

該結構的一個正確的聲明將如下所示

struct potNumber{ 
    int array[20]; 
    char theName[10][20]; 
}; 

,你初始化像這樣:

struct potNumber aPot[3]= 
{ 
    /* 0 */ 
    { 
     {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ }, 
     {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ } 
    }, 
    /* 1 */ 
    { 
     {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ }, 
     {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ } 
    }, 
    /* 2 */ 
    { 
     {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ }, 
     {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ } 
    } 
}; 

但是,我敢肯定這是不是你想要的。理智的方式來做到這一點需要一些樣板代碼:

struct IntArray 
{ 
    size_t elements; 
    int *data; 
}; 

struct String 
{ 
    size_t length; 
    char *data; 
}; 

struct StringArray 
{ 
    size_t elements; 
    struct String *data; 
}; 
/* functions for convenient allocation, element access and copying of Arrays and Strings */ 

struct potNumber 
{ 
    struct IntArray array; 
    struct StringArray theNames; 
}; 

個人而言,我強烈建議不要使用裸露的C數組。通過幫助程序結構和功能執行所有操作,可以讓您從緩衝區中清除/超出和其他問題。每個認真的C編碼器都會隨着時間的推移建立一個類似這樣的東西的分支代碼庫。

+2

好的,謝謝你的幫助,你能否給我一個我如何輸入數據的例子,以及我如何能夠訪問它? – Greg 2011-03-02 20:04:58