Issue with variable declaration

I am having and issue with a variable declaration that I hoping someone call tell me what is happening. The declaration is as follows

byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //Default MAC
byte ip[] = {192,168,168,168}; //Default IP
IPAddress sn;
IPAddress gw;
IPAddress mydns;

char test[] = "test";
char mywebhost[] = "mywebhost";
boolean lastconnected = true;
boolean hostrequest = true;
unsigned long lastwebrequest = 0;
unsigned long webrequestintvl = 60000;
int failedrequests = 0;

void setup {

issue

What issue?

I am having an issue with my variable declaration anything that is directly after the space below mydns doesn't return a value when referenced if I move a different var there it behaves the same and if I move it somewhere else as long as it isn't directly below mydns it works fine. Any help would be appreciated.

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

const uint8_t chipSelect = 4; //SD chip select pin
SdFat sd; //file system object

byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //Default MAC
byte ip[] = {192,168,168,168}; //Default IP
IPAddress sn;
IPAddress gw;
IPAddress mydns;

char test[] = "test";
char mywebhost[] = "mywebhost";
boolean lastconnected = true;
boolean hostrequest = true;
unsigned long lastwebrequest = 0;
unsigned long webrequestintvl = 60000;
int failedrequests = 0;

#define buffermax 50
int buffersize;
char buffer[buffermax];
boolean startbuffer = false;

EthernetServer server(80);
EthernetClient client;

void MyFileRead(char* myfile) {
	Serial.print("My File: ");
	Serial.println(myfile);
	
	const int line_buffer_size = 30;
	char sdbuffer[line_buffer_size];
	ifstream sdin(myfile);

	while (sdin.getline(sdbuffer, line_buffer_size, '\n') || sdin.gcount()) {
		if(strncmp(sdbuffer, "mac", 3) == 0) {
			sscanf(sdbuffer, "mac=%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
		} else if (strncmp(sdbuffer, "ip", 2) == 0) {
			sscanf(sdbuffer, "ip=%u.%u.%u.%u", &ip[0], &ip[1], &ip[2], &ip[3]);
		} else if (strncmp(sdbuffer, "sn", 2) == 0) {
			sscanf(sdbuffer, "sn=%u.%u.%u.%u", &sn[0], &sn[1], &sn[2], &sn[3]);
		} else if (strncmp(sdbuffer, "gw", 2) == 0) {
			sscanf(sdbuffer, "gw=%u.%u.%u.%u", &gw[0], &gw[1], &gw[2], &gw[3]);
		} else if (strncmp(sdbuffer, "dns", 3) == 0) {
			sscanf(sdbuffer, "dns=%u.%u.%u.%u", &mydns[0], &mydns[1], &mydns[2], &mydns[3]);
		}
	}
}

void WebRequest() {
	Serial.print("Requesting: ");
	Serial.println(test);
	Serial.print("Requesting: ");
	Serial.println(mywebhost);
	Serial.println("LastConnected: ");
	Serial.println(lastconnected);
	Serial.println("Request: ");
	Serial.println(hostrequest);
}

void setup() {
	Serial.begin(9600);
	
	delay(500);
	
	Serial.print("Initializing SD card...");
  
	pinMode(10, OUTPUT);

	if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
		Serial.println("Initialization Failed.");
		sd.initErrorHalt();
	}
	
	Serial.println("Initialization Done.");
	
	char myfile[] = "ipconf.txt";
	MyFileRead(myfile);
	WebRequest();
	
	delay(500);
	
	Ethernet.begin(mac,ip,mydns,gw,sn);
	
	Serial.print("IP Address: ");
	Serial.println(Ethernet.localIP()); 
}

void loop() {
}

I'm having an issue with cross-posting.

Sorry about that the first post disappeared from my browser before I could get all the code.

Please excuse the question as I don't have an Ethernet shield, but shouldn't the sn, gw and mydns variable have a value ?

They are getting their values from a text file on an SD card. Eventually I am going to add a check if the values are not present to start ethernet with a default set of values. Just to check that it wasn't messing with my variables I reran the sketch with values and had the same issue.

Don't these need byte, int, long, something, in front of them?

IPAddress sn;
IPAddress gw;
IPAddress mydns;

Just to check that it wasn't messing with my variables I reran the sketch with values and had the same issue.

But you still aren't going to tell us what that issue was? Well, good luck with it. Whatever it is.

PaulS:
But you still aren't going to tell us what that issue was? Well, good luck with it. Whatever it is.

Sorry if I wasn't very clear in the posting of the issue as it is at the top of the code after the double post. To restate the issue if I move the variable after the space below mydns in the code to anywhere else such as above byte ip[] then a Serial.print of the variable will print a value. Whatever variable is directly after that space when doing a serial.print prints nothing or false if it is a boolean variable.

I have tried with sn, gw, dns being byte instead of IPAddress with the same result. Ethernet - Arduino Reference

Upon some other testing this appears to only happen after using sdfat getline. If I move the function WebRequest() before MyFileRead() then the variables appear fine. So it appears that something in MyFileRead() is breaking the variable.

A couple of suggestions. First, print the values of all 5 arrays after each call to sscanf. Perhaps something is stepping on some part of memory that it shouldn't. Determining that that is happening is the first step in solving the problem. Determining where is the second step.

Second, sscanf() returns a value. Save and print it, instead of discarding. Again, a clue by four might present itself.

These are classic symptoms of a buffer overrun. This snippet and others like it are problematic:

sscanf(sdbuffer, "dns=%u.%u.%u.%u", &mydns[0], &mydns[1], &mydns[2], &mydns[3]);

Your intent is to treat mydns as an array of ints, but it has type IPAddress, so you're actually treating it as an array of IPAddress. Indexes 1, 2 and 3 don't exist, so that code is stepping all over whatever variables are next in memory, as you observe.

I am wondering if the byte should be unsigned byte, because if it is a signed byte, the numbers 192 and 168 don't fit.

I am wondering if the byte should be unsigned byte, because if it is a signed byte, the numbers 192 and 168 don't fit.

On an Arduino, byte is unsigned.

Here is the serial output from sscanf.

Initializing SD card...Initialization Done.
My File: ipconf.txt
Returning Scanf mac: 6
mac[0]: 90 mac[1]: A2 mac[2]: DA mac[3]: 0 mac[4]: 4A mac[5]: 7F
Returning Scanf ip: 4
ip[0]: 10 ip[1]: 172 ip[2]: 192 ip[3]: 45
Returning Scanf sn: 4
sn[0]: 255 sn[1]: 255 sn[2]: 255 sn[3]: 0
Returning Scanf gw: 4
gw[0]: 10 gw[1]: 172 gw[2]: 192 gw[3]: 252
Returning Scanf mydns: 4
mydns[0]: 4 mydns[1]: 2 mydns[2]: 2 mydns[3]: 2
Requesting test: 
Requesting mywebhost: mywebhost
Requesting lastconnected: 0
Requesting hostrequest: 1
IP Address: 10.172.192.45

First, print the values of all 5 arrays after each call to sscanf.

Five != One

Yeah I was just showing there was a value in dns[1], dns[2], dns[3] per the previous post. It does appear though that there is something going on with sdbuffer after the first pass through sscanf. As if I try to print it is just looping the SD initialization.

Thanks for your help and patience PaulS. Below is what the serial shows including the buffer after each run through sscanf.

Initializing SD card...Initialization Done.
My File: ipconf.txt
Returning Scanf mac: 6
mac[0]: 90 mac[1]: A2 mac[2]: DA mac[3]: 0 mac[4]: 4A mac[5]: 7F
Buffer: mac=90:A2:DA:00:4A:7F
Returning Scanf ip: 4
ip[0]: 10 ip[1]: 172 ip[2]: 192 ip[3]: 45
Buffer: ip=10.172.192.45
Returning Scanf sn: 4
sn[0]: 255 sn[1]: 255 sn[2]: 255 sn[3]: 0
Buffer: sn=255.255.255.0
Returning Scanf gw: 4
gw[0]: 10 gw[1]: 172 gw[2]: 192 gw[3]: 252
Buffer: gw=10.172.192.252
Returning Scanf mydns: 4
mydns[0]: 4 mydns[1]: 2 mydns[2]: 2 mydns[3]: 2
Buffer: dns=4.2.2.2
Requesting test: 
Requesting mywebhost: mywebhost
Requesting lastconnected: 0
Requesting hostrequest: 1

The reason for printing all 5 arrays after each sscanf is to determine which sscanf is stepping on memory. If the first one that values an IPAddress is the culprit, then the issue is with that sscanf statement.

You have:

IPAddress mydns;

but you also have:

sscanf(sdbuffer, "dns=%u.%u.%u.%u", &mydns[0], &mydns[1], &mydns[2], &mydns[3]);

which, as wildbill pointed out is wrong. mydns is not an array. IPAddress may wrap an array, but it can't be treated as an array. On my Widoze box, I could find the IPAddress.h file, and see how to change your code to work with an IPArress instance. But, I'm on my Mac, and I haven't learned all it's intricacies, yet.

Try changing your sscanf statements to use byte arrays, and then initialize the IPAddress instances AFTER sscanf is done.