It's been days now I'm trying to figure out how arrays work in Arduino IDE. For better understanding, I assembled some pieces of code I found here and there to write this sketch:
#define arrayLength(x) (sizeof(x) / sizeof(int)) //array size
typedef struct {
int note;
int duration;
} BUZZER_NOTE ;
class Buzzer {
public:
BUZZER_NOTE notes[]; //-- Funny how the same declaration tried below DOES work if it's inside a class
};
void setup() {
Serial.begin(9600);
// BUZZER_NOTE notes[]; //-- Compilation error:
int *bob; //-- Declaring this way, shows no compilation errors, but prints out strange values
// int bob[]; //-- Compilation error: "storage size of 'bob' isn't known"
int bob1[] = {10, 20};
int bob2[5] = {10, 20, 30, 40, 50};
int bob3[] = {60, 70, 80};
bob[0] = 10;
bob[1] = 20;
bob[2] = 30;
bob[3] = 40;
bob[4] = 50;
bob[5] = 60;
bob[6] = 70; //-- No matter how many indexes I create, it will always show lenght of 2
test(1); //-- Calls the first function below, as expected
test({1}); //-- Also calls the first function below, when the second one (with the array parameter) is expected
test(bob); //-- Length: 1 (expected 5); Size: 2 (?????) ; First Element: 163 (expected 10);
test(bob1); //-- Length: 1 (expected 2); Size: 2 (?????) ; First Element: 10 (ok);
test(bob2); //-- Length: 1 (expected 5); Size: 2 (?????) ; First Element: 10 (ok);
test(bob3); //-- Length: 1 (expected 3); Size: 2 (?????) ; First Element: 60 (ok);
}
void loop() {
}
void test(int a) {
Serial.println("Called int function");
Serial.println();
}
void test(int a[]) {
Serial.println("Called array function");
Serial.print("Length: ");
Serial.println(arrayLength(a));
Serial.print("Size: ");
Serial.println(sizeof(a));
Serial.print("First Element: ");
Serial.println(a[0]);
Serial.println();
}
The code above prints out these lines:
Called int function // as expected
Called int function // expected array function
Called array function // as expected
Length: 1 // expected 5
Size: 2
First Element: 163 // expected 10
Called array function // as expected
Length: 1 // expected 5
Size: 2
First Element: 10 // as expected
Called array function // as expected
Length: 1 // expected 3
Size: 2
First Element: 10 // as expected
Called array function // as expected
Length: 1 // expected 5
Size: 2
First Element: 60 // as expected
My goal for now is to achieve something like this:
int bob[]; // this gives a compiler error: "storage size of 'bob' isn't known"
bob = {10, 20, 30, 40};
test(bob);
void test(int arr[]) {
for (int i = 0; i < FIND_ARR_LENGTH_SOMEHOW; i++) {
Serial.print("Index i: ");
Serial.println(arr[i]);
}
Question: How can I declare an array without mention its size, fill it up with as many indexes I want, then pass it to a function that will be able to process all the contents?
I know I could declare an array and keep its length to pass as a second parameter, but there must be a way to get an array length.
Thanks in advance.
#define arrayLength(x) (sizeof(x) / sizeof(int)) //array size
is wrong!#define arrayLength(x) (sizeof(x) / sizeof(x[0]))
might work ifx
is an array