0

所以我试图通过执行以下操作来创建一个二维数组:

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

我究竟做错了什么?

4

2 回答 2

1

seq is a Array of type char, you are trying to initialize with address to char.

Replace

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

with

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

Now if you want to read 3rd element of seqA then use:

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

于 2015-03-02T06:25:05.993 回答
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,因为编译器必须生成代码才能将值放在堆栈上。

使用指针解决方案,每次访问都会有四个额外的指针和一个额外的指针取消引用。

于 2015-03-02T09:21:37.760 回答