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
Weil mychar[0][0] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; sieben elemente zuviel sind. mychar[0][0] = 0x00;
> 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 }, .... };
mychar[0] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; funktioniert aber auch nicht! Ich brauche 8 Variablen zu je 8 Elementen.
> 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.
Aber ganz verstehe ich es doch nicht: Einzeilig initialisieren funktioniert nicht, doch wenn man die Elemente einzeln setzt, das klappt doch...
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.
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?
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 | };
|
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;
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.
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
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.