Pages: [1]   Go Down
Author Topic: get the length of an Array  (Read 30083 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is there any function like:

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

to get the size of an array?

Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ;
« Last Edit: June 17, 2008, 12:37:32 pm by mem » Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 1
Posts: 513
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

  #define MY_SIZE 5
  int myarray[MY_SIZE];
  for( int i=0 i <  MY_SIZE; i++)
     myarray = i ;

Is better than

  int myarray[5];
  for( int i=0 i < 5; i++)
     myarray = i ;

Logged

0
Offline Offline
God Member
*****
Karma: 1
Posts: 513
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

#define numUsers (sizeof(users)/sizeof(char *)) //array size  
« Last Edit: June 18, 2008, 08:01:42 am by dcb » Logged

Pages: [1]   Go Up
Jump to: