I don't see any examples of how to check for an arrays length.
In JavaScript you use a method,
len=array.length;
so to illustrate this using JavaScript, what I want to do is...
for( x=0; x<=array.length; x++){
or using a PHP example
for(x=0; x<count(array); x++){
I have looked at an example to an answer here in this forum but that still does not answer my question.
In the response someone said that it is better to define constants for arrays, all well and good, however I want to have a dynamic approach so that I can simply modify an array and not have to trundle through all the code to find elements that need changing. Defining a constant is not always best practice or policy unless the constant has been defined from a function that has found the length of a particular array that is going to be used thoughout the script.
The sizeof() an array (that was a hint) is known when the array is an array.
Arrays are not passed to functions. Pointers are. In the function, there is no way to get the length of the array that the pointer points to, unless the array contains some marker to indicate the end, as the NULL in a char array does.
Arrays don't generally change size.
You either define the size and leave the values empty:
byte arrayData[16];
or define the values, and the size comes from the values:
byte arrayData[] = {0,1,2,3,4,5,6,7,9,10,11,12,13,14,15,};
On the other hand, VirtualWire library receives data and sticks into array, and that can change size. See the examples on how it does that:
Hmm, seems to have a default VW_MAX_MESSAGE_LEN which as I recall is 27 bytes, because of bytes added for manchester encoding & processing time limits.
So I think I'll stick with: you can't change array sizes. Define the biggest you think it will be and don't access outside of it because that accesses other variable locations and weird things happen.
If you need a really big array, '1284P has 16K SRAM, I made a 14625 element array that users can update (once that part of the code is written anyway).
What this does is return the number of elements in the array, where as Paul's gives you the amount of memory allocated to the array. The nice thing about the macro is that it is "typeless", you can use it to figure out the size of any array.
What this does is return the number of elements in the array, where as Paul's gives you the amount of memory allocated to the array. The nice thing about the macro is that it is "typeless", you can use it to figure out the size of any array.
I looked at the sizeOf() function and it doesn't clearly explain anything other than it only returns the number of bytes in the array. So how does (the math) work out, how does sizeof(x) when divided by sizeof(x[0]) result in an array length?
It works by dividing the total number bytes used by the array by the number of bytes of each element of that array.
Hence it gives the number of elements in the array of any kind.
Arrays do change length, especially when you are adding additional things to an existing array
Assuming that were possible (note the use of the subjunctive), you'd know how much you'd added, so you'd still know how big the array was, because to change the length, you'd have had to reallocate the memory for the size of the old array plus the new allocation, so you'd've remembered all that, wouldn't you?
Arrays do change length, especially when you are adding additional things to an existing array
Assuming that were possible (note the use of the subjunctive), you'd know how much you'd added, so you'd still know how big the array was, because to change the length, you'd have had to reallocate the memory for the size of the old array plus the new allocation, so you'd've remembered all that, wouldn't you?
If you have a zero length array and you have a random number that then is used to seed an array with nth elements, you wouldn't know how long it was if data was being added to it or removed throughout the code execution...
Yes, but that still doesn't get around the fact that you can have a zero length array, fill it with a number of elements that are randomly generated or obtained from another source like an input and items can be removed from arrays so the length is not fixed, it can vary over time.
Arrays do change length, especially when you are adding additional things to an existing array.
No, they don't. Not in C++ anyway.
Not arguing but an array can change in length, especially if you have a routine to add data to it!
You are mistaken. As you intimated in your original post, you can do this in Javascript. whether you like it or not, you cannot do it with native C++ arrays.