Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| lehrkraefte:blc:informatik:ffprg2-2020:esp32-arrays [2020/08/21 07:52] – created Ivo Blöchliger | lehrkraefte:blc:informatik:ffprg2-2020:esp32-arrays [2020/08/21 13:47] (current) – Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Arrays in C++ ====== | ||
| + | Arrays in C++ sind nicht so flexibel wie in Python. Z.B. müssen alle Einträge von gleichen Typ sein. Die Grösse wird einmal festgelegt und kann nicht mehr verändert werden (ausser man legt ein neues, grösseres Array an und kopiert die Daten). | ||
| + | |||
| + | <code c++> | ||
| + | // statisch mit Initialisierung (Grösse automatisch) | ||
| + | int ledPins[] = {18, | ||
| + | int numLeds = sizeof(ledPins)/ | ||
| + | |||
| + | int counters[5]; | ||
| + | |||
| + | int *mydata; | ||
| + | int *moredata; | ||
| + | |||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(115200); | ||
| + | delay(200); | ||
| + | | ||
| + | Serial.printf(" | ||
| + | counters[0] = 3; | ||
| + | Serial.printf(" | ||
| + | | ||
| + | mydata = new int[7]; | ||
| + | |||
| + | moredata = new int[10]; | ||
| + | |||
| + | moredata[0] = 42; | ||
| + | |||
| + | // Ein bisschen Hacking: Pointer-Arithmetik | ||
| + | Serial.printf(" | ||
| + | Serial.printf(" | ||
| + | | ||
| + | mydata[11]=23; | ||
| + | | ||
| + | Serial.printf(" | ||
| + | |||
| + | delete[] mydata; | ||
| + | delete[] moredata; | ||
| + | | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // put your main code here, to run repeatedly: | ||
| + | |||
| + | } | ||
| + | </ | ||