0

I'm trying to convert a char array to a char. i used this code

char code[4] = {'1','2','3','4'};

char strcode[4];



void setup()
{
    Serial.begin(9600);
}


void loop()
{
    memcpy(strcode, code, 4);
    Serial.println(strcode);
    Serial.println("done");
    delay(1000);
}

But for some reason when i read my serial monitor i get this:

1234
done

Why are there 2 squares behind it? Am I doing it wrong?

EDIT: For some reason stackexchange doesn't show the 2 squares but there are 2 squares behind 1234

1 Answer 1

3

C strings need to be NUL-terminated.

char strcode[5];

 ...

 ...
     ...
    strcode[4] = '\0';
     ...
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.