2015-03-02 63 views
0

所以我想通過以下操作來創建一個二維數組:C:無法iniitliaze與一維數組二維數組愛特梅爾工作室

unsigned char seqA[] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5}; 
unsigned char seqB[] = {1, 2, 3, 4, 4, 1, 2, 3, 4, 4}; 
unsigned char seqC[] = {3, 2, 1, 5, 4, 3, 2, 1, 5, 4}; 
unsigned char seqD[] = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}; 
unsigned char seq[][10] = {seqA, seqB, seqC, seqD}; 

而且我從試圖獲得大量的錯誤要做到這一點:

Warning 1 missing braces around initializer [-Wmissing-braces] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 2 (near initialization for 'seq[0]') [-Wmissing-braces] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 3 initialization makes integer from pointer without a cast [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 4 (near initialization for 'seq[0][0]') [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 5 initializer element is not computable at load time C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 6 (near initialization for 'seq[0][0]') C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 7 initialization makes integer from pointer without a cast [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 8 (near initialization for 'seq[0][1]') [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 9 initializer element is not computable at load time C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 10 (near initialization for 'seq[0][1]') C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 11 initialization makes integer from pointer without a cast [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 12 (near initialization for 'seq[0][2]') [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 13 initializer element is not computable at load time C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 14 (near initialization for 'seq[0][2]') C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 15 initialization makes integer from pointer without a cast [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Warning 16 (near initialization for 'seq[0][3]') [enabled by default] C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 17 initializer element is not computable at load time C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 
Error 18 (near initialization for 'seq[0][3]') C:\Users\Jonathan\Documents\Atmel Studio\6.2\GccApplication3\GccApplication3\GccApplication3.c 135 1 GccApplication3 

我在做什麼錯了?

回答

1

seqchar類型的數組,您試圖用address to char進行初始化。

更換

unsigned char seq[][10] = {seqA, seqB, seqC, seqD}; 

unsigned char* seq[] = {seqA, seqB, seqC, seqD}; 

現在,如果你想讀的seqA 3元,然後使用:

*(seq[0] +2) OR seq[0][2]

+0

結束了這樣做。謝謝。 – 2015-03-02 06:28:14

+0

爲什麼不只是seq [0] [2]? – jschultz410 2015-03-02 06:30:30

+0

@ jschultz410也沒問題。 – Vagish 2015-03-02 06:33:53

0

除非有特殊原因,命名個人r OWS,最有效的(並且,在我看來,最清晰的)事會

static const unsigned char seq[][10] = 
    { {1, 2, 3, 4, 5, 1, 2, 3, 4, 5}, 
    {1, 2, 3, 4, 4, 1, 2, 3, 4, 4}, 
    {3, 2, 1, 5, 4, 3, 2, 1, 5, 4}, 
    {1, 1, 2, 2, 3, 3, 4, 4, 5, 5} }; 

我還添加了const,其中,提供的數據實在是恆定的,可以幫助優化器在某些做得更好情況和static,這表明seq只在該文件中使用(如果是這種情況)。

如果seq只在一個特定的函數中使用,您可以通過將其移動到該函數中來進一步考慮。在這種情況下,你肯定會想要static,因爲編譯器必須生成代碼才能將值放入堆棧中。

使用指針解決方案,您將有四個額外的指針和一個額外的指針解引用爲每個訪問。