Go Down

Topic: get the length of an Array (Read 34991 times) previous topic - next topic

hires2k

is there any function like:

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

to get the size of an array?


mem

#1
Jun 17, 2008, 07:36 pm Last Edit: Jun 17, 2008, 07:37 pm by mem Reason: 1
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:

[font=monospace]int size = sizeof(myarray) / sizeof(int);[/font]

but a better way is to always use a constant to define the array size, for example:
[font=monospace]
#define MY_SIZE 5
int myarray[MY_SIZE];

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

MysteriousAges

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.

hires2k

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

mem

If by full, you really want to see the full capability of the compiler, have a look at the AVR GCC documentation.  http://www.nongnu.org/avr-libc/user-manual/FAQ.html

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.

dcb

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


mem

Quote
Quote
...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
[font=monospace]
 #define MY_SIZE 5
 int myarray[MY_SIZE];
 for( int i=0 i <  MY_SIZE; i++)
    myarray = i ;
[/font]
Is better than
[font=monospace]
 int myarray[5];
 for( int i=0 i < 5; i++)
    myarray = i ;
[/font]

dcb

#7
Jun 18, 2008, 03:01 pm Last Edit: Jun 18, 2008, 03:01 pm by dcb Reason: 1
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 :)

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

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

Go Up