Go Down

### Topic: How do I declare an array of ip address? (Read 4190 times)previous topic - next topic

#### arduinian

##### Jan 17, 2013, 12:09 amLast 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

#1
##### Jan 17, 2013, 12:11 am
http://www.cplusplus.com/doc/tutorial/arrays/

See the Multidimensional Array section.

#### lloyddean

#2
##### Jan 17, 2013, 12:30 am
Code: [Select]
`byte ip[][4] ={      { 10, 0, 0, 177 }    , { 10, 0, 0, 178 }    , { 10, 0, 0, 179 }};`

#### arduinian

#3
##### Jan 17, 2013, 12:33 amLast 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};`

#### arduinian

#4
##### Jan 17, 2013, 12:35 am

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

#5
##### Jan 17, 2013, 12:45 am

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};`

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

#### arduinian

#6
##### Jan 17, 2013, 12:57 am
I haven't test it out yet so the error that might happen isn't obvious to me.

#### JChristensen

#7
##### Jan 17, 2013, 02:56 am

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 amLast 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);`