Continuing my off-topic question from this topic, post #21... Help with PROGMEM and char arrays - #21 by xfpd
@J-M-L @david_2018 - I did not express my trouble with arrays and pointers, but I think it is time to ask "how" as I am using not-so-clean ways to make argument available in different functions (x/y/z locations and r/g/b colors). So, here is the code that I have been using to try to understand (original is from @J-M-L, I believe), but my comprehension of the terms used lacks. Any "pointers" welcome.
The code...
// https://arduinogetstarted.com/faq/how-to-pass-array-to-function-in-arduino
// https://academy.programmingelectronics.com/pass-array-to-function-in-arduino/
int array_1[] = { 1, 2 };
int length_1 = sizeof(array_1) / sizeof(array_1[0]);
int array_2[] = { 3, 4, 5, 6 };
int length_2 = sizeof(array_2) / sizeof(array_2[0]);
byte thisOne = 0;
void setup() {
Serial.begin(115200);
if (thisOne == 0) {
// pass by ARRAY type
passByArrayType(array_1, length_1);
passByArrayType(array_2, length_2);
} else {
// pass by POINTER type
int pointer_1 = &array_1; // can not pass &array_1 "warning: invalid conversion from 'int' to 'int*'"
int pointer_2 = &array_2; // can not pass &array_2 "warning: invalid conversion from 'int' to 'int*'"
passByPointerType(pointer_1, length_1); // pass the address of array_1 // warning: invalid conversion from 'int' to 'int*' [-fpermissive]
passByPointerType(pointer_2, length_2); // pass the address of array_2 // warning: invalid conversion from 'int' to 'int*' [-fpermissive]
}
}
void loop() {
}
void passByArrayType(int myArray[], int length) {
Serial.print("ARRAY");
for (byte i = 0; i < length; i++) {
Serial.print(myArray[i]);
}
Serial.println();
}
void passByPointerType(int* myPointer, int length) { // note: initializing argument 1 of 'void passByPointerType(int*, int)'
Serial.print("POINTER");
for (byte i = 0; i < length; i++) {
Serial.print(*(myPointer + i));
}
Serial.println();
}
The error(s)...
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2026111-13032-6hfrg1.hfp9j\sketch_feb11a\sketch_feb11a.ino: In function 'void setup()':
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2026111-13032-6hfrg1.hfp9j\sketch_feb11a\sketch_feb11a.ino:20:21: error: invalid conversion from 'int (*)[2]' to 'int' [-fpermissive]
20 | int pointer_1 = &array_1; // can not pass &array_1 "warning: invalid conversion from 'int' to 'int*'"
| ^~~~~~~~
| |
| int (*)[2]
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2026111-13032-6hfrg1.hfp9j\sketch_feb11a\sketch_feb11a.ino:21:21: error: invalid conversion from 'int (*)[4]' to 'int' [-fpermissive]
21 | int pointer_2 = &array_2; // can not pass &array_2 "warning: invalid conversion from 'int' to 'int*'"
| ^~~~~~~~
| |
| int (*)[4]
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2026111-13032-6hfrg1.hfp9j\sketch_feb11a\sketch_feb11a.ino:22:23: error: invalid conversion from 'int' to 'int*' [-fpermissive]
22 | passByPointerType(pointer_1, length_1); // pass the address of array_1 // warning: invalid conversion from 'int' to 'int*' [-fpermissive]
| ^~~~~~~~~
| |
| int
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2026111-13032-6hfrg1.hfp9j\sketch_feb11a\sketch_feb11a.ino:38:29: note: initializing argument 1 of 'void passByPointerType(int*, int)'
38 | void passByPointerType(int* myPointer, int length) { // note: initializing argument 1 of 'void passByPointerType(int*, int)'
| ~~~~~^~~~~~~~~
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2026111-13032-6hfrg1.hfp9j\sketch_feb11a\sketch_feb11a.ino:23:23: error: invalid conversion from 'int' to 'int*' [-fpermissive]
23 | passByPointerType(pointer_2, length_2); // pass the address of array_2 // warning: invalid conversion from 'int' to 'int*' [-fpermissive]
| ^~~~~~~~~
| |
| int
C:\Users\user\AppData\Local\Temp\.arduinoIDE-unsaved2026111-13032-6hfrg1.hfp9j\sketch_feb11a\sketch_feb11a.ino:38:29: note: initializing argument 1 of 'void passByPointerType(int*, int)'
38 | void passByPointerType(int* myPointer, int length) { // note: initializing argument 1 of 'void passByPointerType(int*, int)'
| ~~~~~^~~~~~~~~
exit status 1
Compilation error: invalid conversion from 'int (*)[2]' to 'int' [-fpermissive]
The code on Wokwi does not create an error and both "by array" and "by pointer" produce the expected result:
POINTER 12
POINTER 3456
Every few months I have a go at re-writing this to stop it from producing warnings, but then walk the dog to be more productive.
I have been flipping-through some documents: The C Book — Pointers ... but nothing yet that I understand enough to use. In the mean time, I "solve" my passing problem with globals and not-so-clean means. Thank you for any "pointers."
