Forum: Compiler & IDEs Mehrdimensionales Array


von adds (Gast)


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

von Reiner (Gast)


Lesenswert?

Weil
 mychar[0][0] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

sieben elemente zuviel sind.
 mychar[0][0] = 0x00;

von Karl heinz B. (kbucheg)


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 },
   ....

   };



von adds (Gast)


Lesenswert?

mychar[0] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

funktioniert aber auch nicht!

Ich brauche 8 Variablen zu je 8 Elementen.

von Karl heinz B. (kbucheg)


Lesenswert?

> mychar[0] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

Man kann in C keine Arrays zuweisen.
Wenn du initialisieren willst, dann musst du das als
Initialisierungssyntax bei der Definition des Arrays
angeben. Syntax siehe oben.

von adds (Gast)


Lesenswert?

Achso, man kann das quasi nicht zur Laufzeit festlegen. Schade.

von adds (Gast)


Lesenswert?

Aber ganz verstehe ich es doch nicht:
Einzeilig initialisieren funktioniert nicht, doch wenn man die Elemente 
einzeln setzt, das klappt doch...

von Karl heinz B. (kbucheg)


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.

von adds (Gast)


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?

von Patrick D. (oldbug) Benutzerseite


Lesenswert?

1
typedef struct mychar_s
2
{
3
    uint8_t address;
4
    uint8_t lines[8];
5
} mychar_t;
6
7
mychar_t mychars[8] = {
8
    [0].address = 0x55,
9
    [0].lines = 0xAA,
10
    [1].address = 0x12,
11
    [1].lines = 0x34,
12
    /*...*/
13
};

  

von adds (Gast)


Lesenswert?

Ich bin mir jetzt nicht ganz sicher, ob das gleichwertig ist zu:

mychars[0].address = 0x55;
mychars[0].lines[0] = 0xAA;
mychars[1].address = 0x12;
mychars[1].lines[1] = 0x34;

von Karl H. (kbuchegg)


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.

von adds (Gast)


Lesenswert?

Vielen Dank für die schnelle Hilfe!

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.