Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpSyntax › Using array.length in for loop

Page Index Toggle Pages: 1
Using array.length in for loop (Read 14762 times)
03/07/08 at 16:34:05

hype1   Offline
YaBB Newbies
I Love YaBB 2!

Posts: 16
*
 
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???
 
IP Logged
 
Reply #1 - 03/11/08 at 13:58:17

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
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.
 
IP Logged
 
Reply #2 - 03/12/08 at 08:27:58

hype1   Offline
YaBB Newbies
I Love YaBB 2!

Posts: 16
*
 
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?

Wink
 
IP Logged
 
Reply #3 - 03/12/08 at 13:53:20

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
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.
 
IP Logged
 
Reply #4 - 03/12/08 at 17:58:25

Titus   Offline
Full Member
Wiring Soccer Robot!
Amsterdam

Posts: 176
***
 
All the commands that wiring supports are in this list:
http://wiring.org.co/learning/reference/index.html
Good luck

Titus
 

Check www.TeamVossius.nl for our Autonomous soccer robots!
IP Logged
 
Reply #5 - 03/13/08 at 09:43:54

hype1   Offline
YaBB Newbies
I Love YaBB 2!

Posts: 16
*
 
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?

 
IP Logged
 
Reply #6 - 03/13/08 at 14:50:04

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
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.
 
IP Logged
 
Page Index Toggle Pages: 1