As an arduino and general C/C++ newbie, dealing with arrays area major pain in the side. Actually using them is mostly fine. It's kind of annoying that you have to manually shift everything down if you "delete" a value, but that's beside the point. The real problem (at least for me) is declaring them in functions. Let me give you a few examples of the problems I keep running into:
1. Arrays as parameters
Let's say I have a simple adding function:
int add(int a, int b) {
return a + b;
}
I find myself wanting to add up a bunch of numbers at the same time, so I use arrays to quickly mass-produce addition without having to call the function a trillion times. In order to do this, my reasoning is simple: You make arrays that you don't know the length of by doing int[] someArray;
, so why not do the same here?
With this in mind, I update the add function like so:
int add(int[] numbers) {
int additionThusFar;
for (byte i = 0; i > sizeof(numbers) / sizeof(numbers[0]); i++) {
additionThusFar += numbers[i];
}
return additionThusFar;
}
I pass numbers 1-9 in and Serial print what this function returns, and...
/usr/local/bin/arduino-cli compile --fqbn arduino:avr:uno --build-cache-path /tmp --output-dir /tmp/63609153/build --build-path /tmp/arduino-build-EA7DE48C5522DFA4DDE56F496C67791F /tmp/63609153/sketch_apr28a
/tmp/63609153/sketch_apr28a/sketch_apr28a.ino:5:15: error: expected ',' or '...' before 'numbers'
int add(int[] numbers) {
^~~~~~~
/tmp/63609153/sketch_apr28a/sketch_apr28a.ino:5:15: error: expected ',' or '...' before 'numbers'
int add(int[] numbers) {
^~~~~~~
/tmp/63609153/sketch_apr28a/sketch_apr28a.ino: In function 'int add(int*)':
/tmp/63609153/sketch_apr28a/sketch_apr28a.ino:7:33: error: 'numbers' was not declared in this scope
for (byte i = 0; i > sizeof(numbers) / sizeof(numbers[0]); i++) {
^~~~~~~
/tmp/63609153/sketch_apr28a/sketch_apr28a.ino: In function 'void loop()':
/tmp/63609153/sketch_apr28a/sketch_apr28a.ino:19:41: error: cannot convert '<brace-enclosed initializer list>' to 'int*' for argument '1' to 'int add(int*)'
Serial.println(add({1,2,3,4,5,6,7,8,9}));
^
Error during build: exit status 1
It expected a comma or 3 dots before I made the parameter? Huh??? Of course, I go to try both of those things and it doesn't work. Thanks, compiler...
2. Returning arrays
Ok, let's say that I have this simple adding example again:
int add(int a, int b) {
return a + b;
}
Now, I have a different problem: I find myself needing to add up 10 different sets of numbers. While I could do it manually by doing...
int answer1 = add(0, 1);
int answer2 = add(2,3);
int answer3 = add(4, 5);
...
I don't wanna do that. That's a lotta writing. So instead. I want to just give 2 sets of numbers as parameters and return an array. However, I remember how I still can't figure out how use arrays as parameters thanks to my room temperature IQ, so I use my last 2 braincells to come up with an innovatve, yet extremely wasteful solution: Strings. Now, the only thing standing in my way to unrivaled adding efficiency is the function's return variable declaration. What do I call it? I don't know how many sets of numbers I'm going to add, and I don't want to limit it to a certain amount of choices, so I guess I'll call it int[] add()
? And so, I write the following code:
int[] add(String set1, String set2) { // Set 1 and set 2 have to be the same size, or this won't work lol
int[set1.length()] additionResults;
for (byte i = 0; i > set1.length(); i++) {
additionResults[i] = atoi(set1[i]) + atoi(set2[i]);
}
return additionResults;
}
Like last time, I Serial print my answers, and...
/usr/local/bin/arduino-cli compile --fqbn arduino:avr:uno --build-cache-path /tmp --output-dir /tmp/3181240453/build --build-path /tmp/arduino-build-EA7DE48C5522DFA4DDE56F496C67791F /tmp/3181240453/sketch_apr28a
/tmp/3181240453/sketch_apr28a/sketch_apr28a.ino:5:4: error: decomposition declaration cannot be declared with type 'int'
int[] add(String set1, String set2) { // Set 1 and set 2 have to be the same size or this wont work lol
^~
/tmp/3181240453/sketch_apr28a/sketch_apr28a.ino:5:4: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
/tmp/3181240453/sketch_apr28a/sketch_apr28a.ino:5:4: error: empty decomposition declaration
/tmp/3181240453/sketch_apr28a/sketch_apr28a.ino:5:7: error: expected initializer before 'add'
int[] add(String set1, String set2) { // Set 1 and set 2 have to be the same size or this wont work lol
^~~
/tmp/3181240453/sketch_apr28a/sketch_apr28a.ino: In function 'void loop()':
/tmp/3181240453/sketch_apr28a/sketch_apr28a.ino:19:18: error: 'add' was not declared in this scope
Serial.println(add(String("135"), String("246")));
^~~
/tmp/3181240453/sketch_apr28a/sketch_apr28a.ino:19:18: note: suggested alternative: 'rand'
Serial.println(add(String("135"), String("246")));
^~~
rand
Error during build: exit status 1
Decomposition whatsit? Those words are too big for me to understand. But, after looking deeper into the traceback, I see something about it being mad about the declaration being empty (which I'm assuming means the empty brackets when I first made the function). Well, that sucks-- guess we have to limit how many sets of numbers you can ad at once. And so, I make some edits to my code, and the final revision looks like this:
int[3] add(String set1, String set2) { // Set 1 and set 2 have to be the same size, or this won't work lol
int[set1.length()] additionResults;
for (byte i = 0; i > set1.length(); i++) {
if (i + 1 > 4) {
Serial.println("Too many numbers to add! Only returning the first 3...");
return {additionResults[0], additionResults[1], additionResults[2]};
}
additionResults[i] = atoi(set1[i]) + atoi(set2[i]);
}
return additionResults;
}
And now, I try to run it, and finally it...
/usr/local/bin/arduino-cli compile --fqbn arduino:avr:uno --build-cache-path /tmp --output-dir /tmp/2978091594/build --build-path /tmp/arduino-build-EA7DE48C5522DFA4DDE56F496C67791F /tmp/2978091594/sketch_apr28a
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:5:5: error: expected identifier before numeric constant
int[3] add(String set1, String set2) { // Set 1 and set 2 have to be the same size or this wont work lol
^
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:5:5: error: expected ']' before numeric constant
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:5:4: error: decomposition declaration cannot be declared with type 'int'
int[3] add(String set1, String set2) { // Set 1 and set 2 have to be the same size or this wont work lol
^
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:5:4: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:5:4: error: empty decomposition declaration
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:5:8: error: expected initializer before 'add'
int[3] add(String set1, String set2) { // Set 1 and set 2 have to be the same size or this wont work lol
^~~
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino: In function 'void loop()':
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:23:18: error: 'add' was not declared in this scope
Serial.println(add(String("135"), String("246")));
^~~
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:23:18: note: suggested alternative: 'rand'
Serial.println(add(String("135"), String("246")));
^~~
rand
Error during build: exit status 1
... errors. Who was I kidding here? If this worked then I wouldn't include this as a segment :p. More on the error itself, it sounds like it's trying to sound as sophisticated as it can. "numeric constant"? That sure is a fancy way to say an unchanging integer...
Looking into the error itself again, things get even more confusing. I can't declare integer arrays with an integer? How else am I supposed to declare them?! Expected an ending bracket before the 3? Then what's supposed to be inside the brackets? Friendship and Magic??? I can't leave it blank either because it complains about that as well! Ugh, it just gives me a headache thinking about it...
But there's also something else: a note. I glossed over it on the last one, but now that it popped up again, it's the perfect time to talk about it. To quote:
/tmp/2978091594/sketch_apr28a/sketch_apr28a.ino:5:4: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
Ok, first question: What the heck does it mean to be 'cv qualified'??? Do I need to vet each data type I use in my code for an auto engineering degree before it has the privilege of being used in my simple addition function? This stuff is just way too over my head...
I've been told by many that I overthink things or forget the most basic parts of whatever I'm stuck on, and I'm sure that's what's happening here. I've tried to quickly google search answers to my problems, but either Bing Search sucks or I'm not searching for the right thing because the answers that populate the search results don't actually reflect my problem-- just something close to it. But this just convinces me even more that I'm doing something dumb because the only way for there to be no good answers to a question online is if your question is very unique (which it isn't), or it's something so obviously wrong that there's no reason for anybody to waste their time asking about it in order to fix it. So, where did I screw up? It would make my day if someone could just tell me where I went wrong. I've been looking at this code and the compiler's cryptic warnings for an hour now going crazy trying to figure this out... Of course, I have more problems, like problems with using multidimensional arrays as parameters, but right now I just want to solve this problem 1 step at a time. Thanks for reading this half-help-half-rant thread :).