0

If I want to print something like "Hello" onto my Serial monitor I instead get a certain length of my character array variable instead. Also my integer variables are changing to values that I do not recognize at all.

The issue is coming from using the length() function. But I do not know why.

I want to use this method to convert a string that is input from a user and convert it to a charArray as I then want to save into EEPROM using the EEPROMex library. But this issue is stopping me from going any further.

Here is my code( I am not getting any errors or warnings using this code:

int address = 1;
int input = 2;
int output = 3;
String inputString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned int len = inputString.length() + 1;
char addrChar[] = "a";

void setup() {
  inputString.toCharArray(addrChar, len); 
  Serial.begin(9600);
  Serial.println("address: ");
  Serial.println(address);
  Serial.println("Hello ");
  Serial.println(" ");
  Serial.println("output: ");
  Serial.println(output);
  Serial.println("Goodbye: ");
  Serial.println(" ");
  Serial.println("CharArray: ");
  Serial.println(addrChar);
  Serial.print("Length of CharArray: ");
  Serial.println(inputString.length() + 1);
}

Here is what is printed out onto my serial monitor:

GHIJKLMNOPQRSTUVWXYZ
17989
QRSTUVWXYZ

XYZ
17475
Goodbye:

CharArray;
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Length of CharArray: 27

Why is this happening? Can it be fixed? If so, how? If not, what other/better method can I use to convert a string to a character array?

John B.
  • 15
  • 6
  • Isn't `addrChar` overflowing because it's initialized with the value "a" and takes its size from that single character? `char addrChar[27];` would probably work and so would `char addrChar[len];` *inside* a function, I think. – jDo May 26 '16 at 08:48
  • Ah yeah!Okay so what should I do if the user changes the length of the string? I want to set up a password which the user can change. I would rather not like to set the array size to fix it. Is there a way to change the length of the array according to the users input? – John B. May 26 '16 at 09:05
  • 17475 in binary is "100010001000011". Split this string roughly in the middle and you get "1000011" and "1000100". Convert these two to ASCII and you get "C" and "D". If you do the same with your other output, 17989, you get "E" and "F". See the pattern? You're printing the values at the right addresses (where `input` and `output` used to be) but they have now been overwritten by the value of `addrChar`. – jDo May 26 '16 at 09:24
  • As I wrote in my first comment, do everything from within functions. You could use something like [this sketch](http://stackoverflow.com/a/17051931/6004486) instead and add your stuff to it as needed. – jDo May 26 '16 at 09:40
  • Awesome< I understand now, I am not sure I understand the code you sent but I will figure it out. Thank you very much @jDo – John B. May 26 '16 at 09:59
  • Oh last thing, so why those char addrChar[len]; work in a function but not outside of one? – John B. May 26 '16 at 10:01
  • As all I get is this error: variable-sized object 'addrChar' may not be initialized – John B. May 26 '16 at 10:20
  • I think [this thread](http://forum.arduino.cc/index.php?topic=287019.0) explains it nicely. As the user KenF writes: *"When your sketch is being compiled, the compiler needs to know how much space to reserve for your array. The compiler isn't going to run any code to calculate that value. So it needs to be spoon fed an exact value."* Read about "variable length arrays" to learn more about it - e.g. [here](https://stackoverflow.com/questions/5730101/when-are-variable-length-arrays-legal). It's not a universally supported feature but works in GCC. – jDo May 26 '16 at 10:30
  • *"variable-sized object 'addrChar' may not be initialized"* Remove the `= "a"` part from `char addrChar[len] = "a";` so it says `char addrChar[len];` – jDo May 26 '16 at 10:35

0 Answers0