How do get the array length?

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:

http://www.airspayce.com/mikem/arduino/VirtualWire/examples.html

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).

Paul's right, about the only thing you can do is pass the length in. You will also see this macro:

#define HowBigIsThisArray(x)       (sizeof(x) / sizeof(x[0]))

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.

Maybe the reference page for the sizeof funciton can help you... http://arduino.cc/en/Reference/sizeof

Arrays do change length, especially when you are adding additional things to an existing array.

econjack: Paul's right, about the only thing you can do is pass the length in. You will also see this macro:

#define HowBigIsThisArray(x)       (sizeof(x) / sizeof(x[0]))

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.

No, they don't. Not in C++ anyway.

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?

wildbill:

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!

Not arguing but an array can change in length

I am arguing. You can't.

AWOL:

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...

Do you know what "bollocks" means?

AWOL: Do you know what "bollocks" means?

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.

I’ll tell you what, you post your code to do this.
I haven’t had a good laugh all day.

AWOL: I'll tell you what, you post your code to do this. I haven't had a good laugh all day.

So rather than accept that an array can be any length and it can change in size, you would rather ridicule someone?

I tell you what, you prove to me that this is impossible.

Oh no, I wouldn't want to steal your thunder.

I didn't say it was impossible (some languages allow it; sadly not C), I merely asked you to post your code.

Off you go.

MarkG:

wildbill:

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.

You have an array…

int x[] = {}; // empty array

you make a random number between 50 and 200

long y = random(50, 200);

you run a loop to then fill that array with a random number where the loop is 50 to 200 numbers

for(int c=0; c<=y-50; c++){
 x[c] = random(1, 10);// stick a random number in it 1 to 10
}

When I run that in the Arduino to verify it, I get no errors.

Happy?

Where do you think those random writes went to?

Happy? Me?

Oh yes. Very.

(I can write all sorts of bollocks that compile, but I wouldn't deploy them in a product I had to support)

(Hint: I've been programming in C for very nearly 30 years)