In this sketch I have added 5 values into this array. But its giving 40 as the output of sizeof() function. how to get no of available indexes (5) as my output.
Thank You.
1.sizeof is an operator and not a function. It gives number of bytes present in a data item.
2.long is a data type that refers to 32-bit (4-byte). 3. You have declared an array with 10-member. So, the sizeof(Array1) operator gives: 10x4 = 40 which you have shown onto Serial Monitor.
4. Re-declare the array without showing the dimension (number of items/members present in the array) and then check that sizeof(Array1) returns 20 (5x4). The Compiler is smart enough to count the number of items present in the Array1.
You can't. There is no concept of "unused" or "uninitialized" entries in an array. The ones you don't explicitly initialize are initialized to zero (because global variables are initialized to zero). If you want to keep track of how many entries you have explicitly initialized you will have to do that manually:
long Array1[10] = {321,465564,3214,987546,648454};
size_t Array1Initialized = 5;
Sure, this function returns the size of an arbitrary array.
How this function works will probably not be of interest to a "newbie", we tend not to explain how digitalRead works either, but I can give it a try.
This function deduces the size of an array from its type. It takes a reference to an array of type T[n], an array with n elements of type T. Both T and n are template parameters, T is used to accept arrays with elements of arbitrary type, n is returned.
A new user can go to the Language Reference to find out how to use digitalRead(). It is more difficult to find information on the more esoteric functions and keywords of C++ and how to use them with Arduino.
You do not need to know the internal workings of digitalRead() to use it. It is sufficient to say that it returns the state of the pin number passed to it
I am afraid that the same cannot be said of your explanation of the template because its parameters are at best obtuse. Your explanation uses terms such a "reference" and "type T" neither of which are used in common parlance
I can say with some certainty that I will not be advocating the of such a template in advice to newbies as clever and as useful as it may be
Interestingly, if I copy the code from ArraySize.h directly into the .ino file it causes a compilation error. I've run across that before with templates. I'm guess a conflict with Arduino's auto prototype generation. Moving it to the .h fixes that.
This is getting more and more absurd as time goes on
The query started with a user who wanted to know how to count how many elements there are in an array. He/she was already aware of sizeof() but was confused by what it returned in the case of his/her array
A simple extension of the use of sizeof() and advice to let the compiler determine how many bytes are allocated for the array provided an initial answer the the question
Then we moved on to a solution using a valid but much more convoluted method involving a template
Now we have reached the dizzy heights of putting the template in a .h file and #including it in the sketch
How much more obscure can we make it I wonder ?
Who will be the first person to post a solution using assembly language ?
It's not even clear if he wanted the assigned number of elements in the array or the number of unassigned elements. @johnwasser was the only one who answered the second question.
While the angels are dancing on the head of the pin, perhaps the OP will return and clarify what they wanted.