Array not reporting proper characters

I have this complex (for me) program that takes keypad input and is supposed to parse it into a usable IP address. However, the variable returns strange characters. As per previous requests, I have attached not only the .ino file, but also the supporting libraries that are not in the default configuration.

If I have the function do a serial.println of the data, without any input the data is something like "?>", and putting in 192.168.101.101 results in "192.168.101.1019!S1[!T!M1!" and hangs up the program. Putting in 192.168.1.2 results in "192.168.1.2ยน!M". The only way to get this to work reliably is to put in a very short IP address, like 1.2.3.4.

The program itself is very big, compiling to a size of 32,556 bytes (I'm using a Mega), and the variables are declared within the function, if that helps.

What am I doing wrong?

ArduinoWork.zip (33.7 KB)

What am I doing wrong?

A string is a NULL terminated array of chars. enteredinfo is NOT a string. Passing it to a function that expects a string is just plain wrong. Don't do it.

Make enteredinfo a string by NULL terminating it EVERY time you add a character to it.

PaulS:
A string is a NULL terminated array of chars. enteredinfo is NOT a string. Passing it to a function that expects a string is just plain wrong. Don't do it.
Make enteredinfo a string by NULL terminating it EVERY time you add a character to it.

What I ended up doing was where enteredinfo was being called, I added a second line:

          enteredinfo[count] = key;
          enteredinfo[count+1] = '\0'; // trying to add a null at the end

That seemed to work. :slight_smile:

FTPMonster:

PaulS:
A string is a NULL terminated array of chars. enteredinfo is NOT a string. Passing it to a function that expects a string is just plain wrong. Don't do it.
Make enteredinfo a string by NULL terminating it EVERY time you add a character to it.

What I ended up doing was where enteredinfo was being called, I added a second line:

          enteredinfo[count] = key;

enteredinfo[count+1] = '\0'; // trying to add a null at the end




That seemed to work. :)

Yep, and this is one of the best methods. Just remember to allocate 1 more array element than characters you expect so there is room for the count+1 to be set to 0x00 (same thing as NULL and '\0').

Another method would be to use a for loop to set every element of the array to 0x00 right before you start accumulating data into it. Both are valid, your choice which ever method you use.

Another method would be to use a for loop to set every element of the array to 0x00 right before you start accumulating data into it. Both are valid, your choice which ever method you use.

One method shows that you understand what NULL-terminated means. The other does not. I know which I prefer.