Go Down

Topic: How do you check the Length of an Array? (Read 2 times) previous topic - next topic

wh33t

I was just curious if there was a way to check the length of an array?

I didn't see any direct answers to this on the forums and the Array function page on the website didn't say anything about checking the length either.

I was hoping to see something like sizeof(array_name), but sizeof() doesn't work that way with Arduino does it?

dxw00d

Usually, you will have created the array, so you'll know the length of it.

wh33t

So you must always keep track of it's length with another variable then?

dxw00d

What exactly do you mean by length. Arrays are fixed at the length you define them.

wh33t

Would there not ever be a time where you would want to add things into an array during Run Time?

I'm still just starting out with Arduino but I have a php programming background.

I'm going through this lesson booklet I have and the one I'm currently on was to set up 8 blinking LEDs.
As the lesson book suggested I stored them in an array

Code: [Select]
int ledPins[] = {2,3,4,5,6,7,8,9}

Then in the set program it suggests to loop through them like this

Code: [Select]
void setup()
{
  for(int i = 0; i < 8; i++)
  {
    pinMode(ledPins[i], OUTPUT);
  }
}


Whereas I, in PHP would done something like

Code: [Select]
void setup()
{
  for(int i = 0; i < sizeof(ledPins); i++)
  {
    pinMode(ledPins[i], OUTPUT);
  }
}


Like I said, I'm still starting out and just trying to get used to the language so I'm not actually sure if there would ever be a case where an Array might grow beyond it's declared size during run time but I figured this would be common.

Go Up