所以我试图通过执行以下操作来创建2D数组:
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
我做错了什么?
答案 0 :(得分:1)
seq
是char
类型的数组,您尝试使用address to char
进行初始化。
替换
unsigned char seq[][10] = {seqA, seqB, seqC, seqD};
与
unsigned char* seq[] = {seqA, seqB, seqC, seqD};
现在,如果您想阅读seqA
的第3个元素,请使用:
*(seq[0] +2)
或seq[0][2]
答案 1 :(得分:0)
除非有特定的理由来命名各行,否则最有效(而且,在我看来,最清楚)的事情将是
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
,因为编译器必须生成代码才能将值放在堆栈上。
使用指针解决方案,每次访问都会有四个额外的指针和一个额外的指针解除引用。