Hallo zusammen,
ich habe lokale typedef struct in setup(), auf die ich von loop() aus per call by reference zugreifen möchte. Wäre die typedef struct als globale variable definiert, also vor setup() wäre der Zugriff ja kein Problem. Aber das ging nicht, weil die Größe erst berechnet werden mußte. Und dies funktioniert erst ab setup().
Hier mein Code:
byte noOfSetBits = 0; // Placeholder
const byte bitsSet = B00111001; // Set bits)
typedef struct {
byte byteOne; //one byte
byte byteTwo; //one byte
float floatOne; // four bytes
float floatTwo; // four bytes
} STRUCTDEF; // Sum = 10 bytes
STRUCTDEF *pstructVar;
void setup() {
Serial.begin(74880);
Serial.print("\n\nbitsSet = ");
Serial.print(bitsSet, BIN);
Serial.print("\nnoOfSetBits before calculating = ");
Serial.print(noOfSetBits);
noOfSetBits = countSetBits(bitsSet); // fill placeholder with actual no of bits set
Serial.print("\nnoOfSetBits calculated = ");
Serial.print(noOfSetBits);
STRUCTDEF structVar[noOfSetBits]; // Allocate mem array of struct
//pstructVar = &structVar[0];
pstructVar = structVar;
Serial.print("\nSize of typedef STRUCTDEF array structVar[] = ");
Serial.print(sizeof(structVar));
Serial.print("\nSize of typedef STRUCTDEF array structVar[0] = ");
Serial.print(sizeof(structVar[0]));
// fill structVar[]
for (int i = 0; i < noOfSetBits; i++) {
structVar[i] = {i, 2 * i, 3 * i, 4 * i};
}
Serial.print("\n\nFilled structVar[0] ... [3] in Setup()\n");
for (int i = 0; i < noOfSetBits; i++) {
Serial.print(structVar[i].byteOne);
Serial.print(", ");
Serial.print(structVar[i].byteTwo);
Serial.print(", ");
Serial.print(structVar[i].floatOne);
Serial.print(", ");
Serial.println(structVar[i].floatTwo);
}
}
void loop() {
Serial.print("\n\nvoid loop(): Size of typedef STRUCTDEF array structVar[] = ");
Serial.print(sizeof(pstructVar));
Serial.print("\nvoid loop(): Size of typedef STRUCTDEF array structVar[0] = ");
Serial.print(sizeof(pstructVar[0]));
Serial.print("\n\nFilled structVar[0] ... [3] in loop()\n");
for (int i = 0; i < noOfSetBits; i++) {
Serial.print(pstructVar[i].byteOne);
Serial.print(", ");
Serial.print(pstructVar[i].byteTwo);
Serial.print(", ");
Serial.print(pstructVar[i].floatOne);
Serial.print(", ");
Serial.println(pstructVar[i].floatTwo);
}
while (1);
}
//******* functions *******
int countSetBits(byte number) {
int result = 0;
for (int i = 0; i < 8; i++) {
result += (number & 1);
number >>= 1;
}
return result;
}
Und hier der Ergebnisprint:
bitsSet = 111001
noOfSetBits before calculating = 0
noOfSetBits calculated = 4
Size of typedef STRUCTDEF array structVar[] = 40
Size of typedef STRUCTDEF array structVar[0] = 10
Filled structVar[0] ... [3] in Setup()
0, 0, 0.00, 0.00
1, 2, 3.00, 4.00
2, 4, 6.00, 8.00
3, 6, 9.00, 12.00
void loop(): Size of typedef STRUCTDEF array structVar[] = 2
void loop(): Size of typedef STRUCTDEF array structVar[0] = 10
structVar[0] ... [3] in loop()
0, 0, 0.00, 0.00
26, 1, 0.00, 0.00
1, 19, ovf, ovf
8, 251, 0.00, ovf
Die zweite Tabelle, aus loop() heraus ausgegeben, sollten identische Werte zur ersten aus setup() anzeigen, also:
0, 0, 0.00, 0.00
1, 2, 3.00, 4.00
2, 4, 6.00, 8.00
3, 6, 9.00, 12.00
Tut sie aber nicht!
Was mache ich falsch?
Hilfreiche Tipps wären mir bei meiner Pointerverwirrung sehr hilfreich.