0

そこで、次のようにして 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

私は何を間違っていますか?

4

2 に答える 2

1

seqタイプの配列です。charで初期化しようとしていますaddress to char

交換

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

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

の 3 番目の要素を読み取りたい場合は、seqA次を使用します。

*(seq[0] +2) また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が 1 つの特定の関数内でのみ使用されている場合は、その関数に移動することでさらに進めることができます。その場合static、コンパイラはコードを生成してスタックに値を配置する必要があるため、間違いなく必要です。

ポインター ソリューションを使用すると、アクセスごとに 4 つの追加ポインターと追加のポインター逆参照が得られます。

于 2015-03-02T09:21:37.760 に答える