Go Down

Topic: How do I declare an array of ip address? (Read 1 time) previous topic - next topic

arduinian

Jan 17, 2013, 12:09 am Last Edit: Jan 17, 2013, 03:27 pm by arduinian Reason: 1
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.

Arrch


lloyddean

Code: [Select]

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


arduinian

#3
Jan 17, 2013, 12:33 am Last Edit: Jan 17, 2013, 12:37 am by arduinian Reason: 1
Ok, so if I have 3 IP addresses,is the following declaration correct?

Code: [Select]

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

arduinian


Code: [Select]

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




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

Arrch


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

Code: [Select]

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.

arduinian

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

Jack Christensen

How about

Code: [Select]
#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)
{
}

zoomkat

#8
Jan 17, 2013, 06:18 am Last Edit: Jan 17, 2013, 06:20 am by zoomkat Reason: 1
I've seen code that had clients set like below.

Code: [Select]

Client client1(ip, port);
Client client2(ip2, port2);
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy