get the length of an Array

is there any function like:

int myarray[5];
int test = myarray.length(); // RETURN TEST = 5

to get the size of an array?

In C you can get the size of your array in bytes using sizeof(myarray);
To get the number of elements you divide this by the size of a single element, like this:

int size = sizeof(myarray) / sizeof(int);

but a better way is to always use a constant to define the array size, for example:

#define MY_SIZE 5
int myarray[MY_SIZE];

for( int i=0 i < MY_SIZE; i++)
myarray = i ;
[/font]

1 Like

One thing to note about the sizeof function is that it will not work on arrays not defined at compile time.

char * arr;
arr = new char[10];
int size = sizeof(arr);

size will contain 2, which is the size of a char *.

The arr.length() function you speak of doesn't exist in C++. Java and other languages actually build a class around the array to provide the programmer with all these handy little functions, and also provide some bounds-checking as well, preventing you from accidentally doing arr[10] when the array is only 10 items long. It is possible to create a 'wrapper class' in C++ to get these nice features, but you have to use it explicitly.

thanks a lot! arduino is still new for me and every day i start geting crazy with it, normaly i work with actionscript, php and java

is somewehre a full syntax reference of arduino available ?

greetz martin

If by full, you really want to see the full capability of the compiler, have a look at the AVR GCC documentation. Frequently Asked Questions

BTW, 'new' and 'delete' are not implemented on the compiler used by the arduino, but mysteriousAges point is valid, taking sizeof a pointer to an array returns the size of the pointer, not the array its pointing to.

...but a better way is to always use a constant to define the array size, for example: ...

Not to be too much of a nitpick, but I don't think that it is always "better". Sometimes you really do just want to be able to add add a string/value/object in an array initializer and let the computer figure out how many of them there really are. sizeof is annoying but you only have to get it right once.

...but a better way is to always use a constant to define the array size, for example: ...

Not to be too much of a nitpick, but I don't think that it is always "better". Sometimes you really do just want to be able to add add a string/value/object in an array initializer and let the computer figure out how many of them there really are. sizeof is annoying but you only have to get it right once.

Hi DCB, since we're nitpicking, the point was that a constant is better than a value inserted into the body of the code as per the OP's fragment.

i.e. the code in reply #1

#define MY_SIZE 5
int myarray[MY_SIZE];
for( int i=0 i < MY_SIZE; i++)
myarray = i ;
[/font]
Is better than

  • int myarray[5];*
  • for( int i=0 i < 5; i++)*
    _ myarray = i ;_
    [/font]

Yup, I was thinking specifically of those little lists of things that sometimes get hardcoded but their associated length variables are occasionally overlooked. It is something of a niche, but I miss array.length() too whenever I'm using C and it is as close as I can get :slight_smile:

char * users[]={
"tom",
"dick",
"harry"
};

#define numUsers (sizeof(users)/sizeof(char *)) //array size