Trying to declare an IP using a variable

This is part of a larger project i'm working with where a user inputs the last byte of an IP for an arduino to send a UDP message to. I've stripped out the relevant code to a simple test program:

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

int y=45;
IPAddress ip=(192,168,2,y);

void setup(){
  Serial.begin(9600);
  Serial.println(ip);
}
void loop(){}

When run i get the following from the serial console:"45.0.0.0", when what i want to be getting is "192.168.2.145". Clearly i'm not addressing this right. Any ideas how to do it?

For info i'm using IDE 1.0 and an UNO with an Ethernet Shield (not that the board and shield should affect it)

Quick answers appreciated, this is for a coursework project with a fast approaching deadline.

Thanks,

Jamesterjlrb

The ip is a 4 byte array variable. Try something like this:

Serial.print(ip[0],DEC);
Serial.print(".");
Serial.print(ip[1],DEC);
Serial.print(".");
Serial.print(ip[2],DEC);
Serial.print(".");
Serial.println(ip[3],DEC);

You could use a loop if you wanted to be cleaner.

SurferTim:
The ip is a 4 byte array variable. Try something like this:

Serial.print(ip[0],DEC);

Serial.print(".");
Serial.print(ip[1],DEC);
Serial.print(".");
Serial.print(ip[2],DEC);
Serial.print(".");
Serial.println(ip[3],DEC);



You could use a loop if you wanted to be cleaner.

Produces the same result. Further when i tried the above declaration in the main project it didn't appear to be sending messages out to the right IPs.

You should do this:

IPAddress ip(192,168,2,y);

The value of

(192,168,2,y)

is simply y, because you're using the comma operator. So you're saying: ip = y
My version says "call the constructor for IPAddress with 4 values"

Also, should y be 145?

WizenedEE:
You should do this:

IPAddress ip(192,168,2,y);

The value of

(192,168,2,y)

is simply y, because you're using the comma operator. So you're saying: ip = y
My version says "call the constructor for IPAddress with 4 values"

Also, should y be 145?

Thank you very much, this works perfectly, its annoying i didn't realise this. y was just a random number to prove my point.

I'm now getting a problem where i'm trying to store said IPs in an array e.g.

IPAddress MIP[5];
int x=0;
MIP[x](192,168,2,y);

I get the following error message:
error: no match for call to '(IPAddress) (int, int, int, int&)'

I assume it's something to do with the fact i'm trying to shove an array into an array.

I think the problem is that you are trying to call a constructor after the (static) variable has been declared. I believe this isn't allowed.