C:无法iniitliaze与一维数组二维数组爱特梅尔工作室

问题描述:

所以我想通过以下操作来创建一个二维数组: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 

我在做什么错了?

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

除非有特殊原因,命名个人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,因为编译器必须生成代码才能将值放入堆栈中。

使用指针解决方案,您将有四个额外的指针和一个额外的指针解引用为每个访问。