How do I declare an array of ip address?

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.

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

See the Multidimensional Array section.

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

Ok, so if I have 3 IP addresses,is the following declaration correct?

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! :slight_smile:

lloyddean:

byte ip[][4] =

{
      { 10, 0, 0, 177 }
    , { 10, 0, 0, 178 }
    , { 10, 0, 0, 179 }
};

Hi lloyddean, that's even more succinct!
Thank you! :slight_smile:

arduinian:
Ok, so if I have 3 IP addresses,is the following declaration correct?

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! :)

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

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

How about

#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)
{
}
1 Like

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

Client client1(ip, port);
Client client2(ip2, port2);