1
void alarm() {
  char keypressed4;
  char keypressed5;
  char keypressed6;
  char keypressed7;
  char disarmcode1[5];
  Serial.println("hi2");
  Serial.println(disarmcode1);

The array gets declared here and I print the values in the empty array. In the serial monitor I read this:

hi2
"c

I don't know why or how these values get put into the array. I have code later on that puts values into the array, but from where that point in the code is, no values should be in the array

Also I have an LCD screen hooked up too, and it prints a value of 0 at (2, 0).

  • 2
    That is why you usually initialize stuff with = 0 and = { 0 } for arrays respectively. – mystery Mar 26 '18 at 13:15
5

The values haven't been "put" in the array. The array has been located in memory where those values (left over from some earlier operation, possibly as part of a function's stack frame) happen to be.

The simple fact that you haven't put anything in there means that what was there before hasn't been changed.

  • How would i clear a array of these values or make them equal NULL then. im aware of in java you can declare they array with its values and make it equal null but null works differently in the C language – Stephen Sanichara Mar 26 '18 at 13:15
  • while (keypressed4 == NO_KEY) { keypressed4 = keypad1.getKey(); } Serial.println("hi2"); if (keypressed4 != NO_KEY) { disarmcode1[0] = keypressed4; lcd.setCursor(0, 0); lcd.print(keypressed4); } after the serial.println disarm code is this and i need a clean array for this portion of the code to work how would i achieve this? – Stephen Sanichara Mar 26 '18 at 13:16
  • You can use memset: memset(discarmode, 0, 5); which sets the 5 bytes of memory to 0, or you can use an initialisation list: char discarmode[5] = {0}; or explicitly for all values: char discarmode[5] = {0, 0, 0, 0, 0}; – Majenko Mar 26 '18 at 13:20
  • void alarm() { char keypressed4; char keypressed5; char keypressed6; char keypressed7; char disarmcode1[5]={0}; Serial.println("hi2"); Serial.println(disarmcode1); while (keypressed4 == NO_KEY) { keypressed4 = keypad1.getKey(); } Serial.println("hi2"); if (keypressed4 != NO_KEY) { disarmcode1[0] = keypressed4; lcd.setCursor(0, 0); lcd.print(keypressed4); } okay so this portion from the while loop to the if statment happens 3 more times to record 4 values which will be compared to a code example 1234, == keypad recorded values of 1234. – Stephen Sanichara Mar 26 '18 at 13:29
  • i did this in a earlier function in the code and it worked prefect. by changing the char disarmcode1[5]; to char disarmcode1[5]={0}; it now apperares as blanks which is good. but i no longer can record values for the input and it leads for the value of the array after recording to hi2 hi2 hi2 hi2 1!N *** this happens after the 4 keypad values should have been recorded it skips right by it and outputs this – Stephen Sanichara Mar 26 '18 at 13:31
2

local arrays are not initialized. Reading uninitialized values is Undefined Behavior.

Practically this means that when read they will interpret the bytes at their memory location as values which can result in anything.

1

Ad to existing answers, while the default behavior of reading unassigned non-instance and non-static memory (e.g. local array) is undefined, you can use slightly different C++ declaration which is defined as initialized to all zeroes:

char disarmcode1[5] = {};

or in pure C

char disarmcode1[5] = {0};
  • An obsolete syntax for GCC even allows define ranges and their values for array initialization. – wondra Mar 26 '18 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.