Pages: [1]   Go Down
Author Topic: How do I declare an array of ip address?  (Read 743 times)
0 Members and 1 Guest are viewing this topic.
Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the Ethernet code sample, an IP address is declared like this:
byte ip[] = { 10, 0, 0, 177 };

How do I declared an array of them? I would like to be able to loop through the array and access them one by one using their index. Adding a length into the square bracket doesn't seemed to work.
« Last Edit: January 17, 2013, 09:27:06 am by arduinian » Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.cplusplus.com/doc/tutorial/arrays/

See the Multidimensional Array section.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
byte ip[][4] =
{
      { 10, 0, 0, 177 }
    , { 10, 0, 0, 178 }
    , { 10, 0, 0, 179 }
};
Logged

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

Ok, so if I have 3 IP addresses,is the following declaration correct?
 
Code:
byte ips [3][4];
ips [0] = {192, 168, 1, 1};
ips [1] = {192, 168, 1, 2};
ips [2] = {192, 168, 1, 3};

Thanks for the link! smiley
« Last Edit: January 16, 2013, 06:37:11 pm by arduinian » Logged

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

Code:
byte ip[][4] =
{
      { 10, 0, 0, 177 }
    , { 10, 0, 0, 178 }
    , { 10, 0, 0, 179 }
};


Hi lloyddean, that's even more succinct!
Thank you! smiley
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so if I have 3 IP addresses,is the following declaration correct?
 
Code:
byte ips [3][4];
ips [0] = {192, 168, 1, 1};
ips [1] = {192, 168, 1, 2};
ips [2] = {192, 168, 1, 3};

Thanks for the link! smiley


No. Assigning values to an array using curly braces is only available when you declare the array, as  lloyd demonstrated.
Logged

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

I haven't test it out yet so the error that might happen isn't obvious to me.
Thanks for the heads up! smiley
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3955
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How about

Code:
#include <Ethernet.h>
#include <SPI.h>

IPAddress myIP[3] = { IPAddress(1,2,3,4), IPAddress(5,6,7,8), IPAddress(9,10,11,12) };

void setup(void)
{
    Serial.begin(115200);
    Serial.println(myIP[0]);
    Serial.println(myIP[1]);
    Serial.println(myIP[2]);
}

void loop(void)
{
}
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've seen code that had clients set like below.

Code:
Client client1(ip, port);
Client client2(ip2, port2);
« Last Edit: January 17, 2013, 12:20:44 am by zoomkat » Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: