C:无法在 Atmel Studios 中使用 1D 阵列启动 2D 阵列



所以我正在尝试通过执行以下操作来创建 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:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 2   (near initialization for 'seq[0]') [-Wmissing-braces]   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 3   initialization makes integer from pointer without a cast [enabled by default]   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 4   (near initialization for 'seq[0][0]') [enabled by default]  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   5   initializer element is not computable at load time  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   6   (near initialization for 'seq[0][0]')   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 7   initialization makes integer from pointer without a cast [enabled by default]   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 8   (near initialization for 'seq[0][1]') [enabled by default]  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   9   initializer element is not computable at load time  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   10  (near initialization for 'seq[0][1]')   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 11  initialization makes integer from pointer without a cast [enabled by default]   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 12  (near initialization for 'seq[0][2]') [enabled by default]  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   13  initializer element is not computable at load time  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   14  (near initialization for 'seq[0][2]')   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 15  initialization makes integer from pointer without a cast [enabled by default]   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Warning 16  (near initialization for 'seq[0][3]') [enabled by default]  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   17  initializer element is not computable at load time  C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3
Error   18  (near initialization for 'seq[0][3]')   C:UsersJonathanDocumentsAtmel Studio6.2GccApplication3GccApplication3GccApplication3.c  135 1   GccApplication3

我做错了什么?

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]

除非有特定的原因来命名各个行,否则最有效(在我看来,也是最清晰(的事情是

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因为编译器必须生成代码才能将值放在堆栈上。

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

最新更新