Mehrdimensionales Array

Gast #441732
Lesenswert?

Warum akzeptiert der avr-gcc foldenden Code nicht?

uint8_t mychar[8][8];
mychar[0][0] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
mychar[1][0] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};

Ich erhalte folgende Meldungen:

test.c:6: error: conflicting types for ‘mychar’
test.c:4: error: previous declaration of ‘mychar’ was here
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar[0]’)
test.c:6: warning: excess elements in array initializer
test.c:6: warning: (near initialization for ‘mychar’)
test.c:6: warning: data definition has no type or storage class
test.c:7: error: conflicting types for ‘mychar’
test.c:4: error: previous declaration of ‘mychar’ was here
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[0]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[1]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[1]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[1]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[1]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[1]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[1]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar[1]’)
test.c:7: warning: excess elements in array initializer
test.c:7: warning: (near initialization for ‘mychar’)
test.c:7: warning: data definition has no type or storage class
#441739
Lesenswert?

> test.c:6: warning: excess elements in array initializer
> test.c:6: warning: (near initialization for ‘mychar[0]’)

Willst du das Array initialisieren.

Dann mach das so:

uint8_t mychar[8][8] = {
   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
   { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
   ....

   };


#441823
Lesenswert?

Initiailisieren ist nicht gleich Zuweisung!!

Auch wenn da ein '=' Zeichen geschrieben wird.

Initialisierung passiert immer bei der Definition
einer Variablen.
Alles andere danach ist immer eine Zuweisung.

Das heist, hier ...

   int i;
   i = 5;

... hier weist du i den Wert 5 zu (das ist eine Zuweisung).

Während hier ...

    int i = 5;

... hier wird i mit dem Wert 5 initialisiert.
Auch wenn beides ähnlich ausschaut, ist das konzeptionell
was völlig anderes.

Soweit als Vorgeplänkel.
Arrays kann man in C nicht als ganzes Zuweisen. Es gibt
keine Syntax dafür. Daher geht das

    int i[2];
    i = { 5, 6 };

nicht. Denn das ist ja eine Zuweisung.

Mann kann aber Arrays initialisieren, dafür gibt es eine
Syntax:

   int i[2] = { 5, 6 };

Das ist eine Initialisierung, daher geht das.
Gast #442221
Lesenswert?

OK, danke für die Erklärung!

Jetzt hätte ich noch eine Frage:

Wenn ich nun sowas wie

typedef struct mychar_
{
    uint8_t address;
    uint8_t lines[8];
}mychar;
mychar mychars[8];

benötige, kann ich auch dieses Struktur-Array auf ähnliche Weise 
INITIALISIEREN?
#442262
Lesenswert?

Die traditionelle Schreibweise wäre:

typedef struct mychar_
{
    uint8_t address;
    uint8_t lines[8];
}mychar;

mychar mychars[8] = {
     { 0x55, { 0xAA, 0xBB, 0xCC } },
     { 0x56, { 0xCC, 0xDD, 0xEE, 0xFF } },
     { ...
};

Für jede Schachtelungstiefe einfach ein { } Pärchen.

Die von Patrick vorgeschlagene neuere Schreibweise verwende
ich (wenn überhaupt) nur dann, wenn ich aus einer Struktur
nur einige Felder initialisieren will.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren