The statements below (which are data definitions, not declarations):
// Definition **
int myArrayActual[1]; // Array for all digital inputs
int myArrayLast[2] = {0,0}; // Array to compare with other array
asks for an array with one element, which is no different than a plain int, while the second definition asks for a two element array. The sizeof operator returns the total amount of memory allocated to the data item. Therefore, your first expression asking for the size of myArrayActual[] should return the size of a single int...2 bytes. The size of myArrayLast[] has two elements, so it returns 4 bytes.
Arrch has it correct. You are confusing the bytes allocated for the array with the number of elements in the array:
myArray[ ? ] = {0,0};
sizeof(myArray()); // =2
sizeof(myArray) return the bytes allocated to myArray[], which is 2 bytes for each element, or 4 bytes total. The macro Arrch showed you takes the memory allocated to the array (4 bytes) and divides it by the amount of memory needed for each element in the array (2 bytes) with the result being the element count for the array (2). Therefore:
long int biggerArray[] = {1,2,3,4,5,6,7,8,9,0};
using the macro would resolve to:
sizeof(biggerArray) / sizeof(biggerArray[0])
40 / 4
10 elements in the array, since each long requires 4 bytes of storage.
Actually, I am wrong. I was thinking of classes. I have been working in .NET too much, thinking of all data types as derivative of Object. Plus I wrote int[0] instead of int[1], ha.
int i;
int i[1];
Those are the same. michinyon is incorrect. Both allocate 2 bytes of memory and add a pointer to the stack.
Isn't all this a bit pointless in the small Arduino environment?
As the programmer you decide what size the array will be so why is there a need to use sizeof().
If you need to use the size of the array in a FOR loop, for example just use
Robin2:
Isn't all this a bit pointless in the small Arduino environment?
Why does the size of the Arduino environment matter? It all gets optimized to the same values anyway.
Robin2:
As the programmer you decide what size the array will be so why is there a need to use sizeof().
So I need to update as little as possible if I need to make changes. Using the above mentioned macro scales very well with multiple arrays, particularly if the size of any of those arrays could change often between code uploads.
Isn't all this a bit pointless in the small Arduino environment?
As the programmer you decide what size the array will be so why is there a need to use sizeof().
If you need to use the size of the array in a FOR loop, for example just use
const byte arrayLen = 6;
int myArray[arrayLen];
I disagree strongly. The sizeof operator offers numerous advantages. Using the Array_Size() macro is typeless, so if I want to march through a for loop using an int array, I can use the macro. If I want to traverse a different array of type float, I can use the same macro because it is typeless. Your way requires defining a const for each array used in the program if they have different element counts. Also, if I change the size of the array, I need to remember to edit the length constant, too. The macro obviates such changes. Finally, the macro allows you to get away from "magic numbers" (e.g., 6) in the code.