Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Syntax >> Using array.length in for loop
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1204907645

Message started by hype1 on 03/07/08 at 16:34:05

Title: Using array.length in for loop
Post by hype1 on 03/07/08 at 16:34:05
Hi,

I have a problem accessing an arrays length in Wiring. I have been using processing and Actionscript for a while now, and I never had problems accessing an array's length.  I think it has something to do with me writing a wrong Wiring syntax here...

Please have a look at my code:

int ledPinBlink = 48;
int ledPins[] = { 0, 1, 2, 3, 4, 5, 6, 7 };

void setup() {
 pinMode(ledPinBlink, OUTPUT);
 for (int i=0; i<ledPins.length; i++) {
   pinMode(ledPins[i], OUTPUT);
 }
}

This code produces the following error when verifying:

In function 'void setup()':
error: request for member 'length' in 'ledPins', which is of non-class type 'int [8]

It sais that the ledPins is not a classtype int[8]. In the [] it sais the length of the array. If I am right, I declaired the variable as an int array right. I am confused... Can someone help me out here???

Title: Re: Using array.length in for loop
Post by Alan_Kilian on 03/11/08 at 13:58:17
It's not Java or C++ at this level, so you can't use the .length construct.

You need to use sizeof(ledPins)/sizeof(ledPins[0]) instead.

for (int i=0; i<sizeof(ledPins)/sizeof(ledPins[0]); i++) {
}

I know it's yucky, but that's the language.

Title: Re: Using array.length in for loop
Post by hype1 on 03/12/08 at 08:27:58
Thank you for this Alan. What kinda language is it then, just Wiring?

I can't find sizeof() in the language refference. Also I saw in Wiring that length is recognized as a part of the language, because Wiring colors that word. Length I can't find in the reference to. Is there an extended version of the reference somewhere? Or must I know something I just don't know yet?

;)

Title: Re: Using array.length in for loop
Post by Alan_Kilian on 03/12/08 at 13:53:20
I think there's C++ "under the covers", but I just write in regular old
C and that seems to work out fine.

I get a whiff of Java every once in a while also, but I don't know
where that comes from.

I think if you look into Processing, there may be more documentation.

Title: Re: Using array.length in for loop
Post by Titus on 03/12/08 at 17:58:25
All the commands that wiring supports are in this list:
http://wiring.org.co/learning/reference/index.html
Good luck

Titus

Title: Re: Using array.length in for loop
Post by hype1 on 03/13/08 at 09:43:54
Hi Titus,

The command sizeof() hasn't been documented there. I wonder what else we can use.

Can somebody tell me if there is more commands we can use than listed in the reference?


Title: Re: Using array.length in for loop
Post by Alan_Kilian on 03/13/08 at 14:50:04
Things like sizeof() are a part of a standard C implementation, and are
usually not documented in the specific implementation.

Look at some standard C language documentation to learn more.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.