dimanche 9 février 2020

Character arrays declared sequentially overlapping

I am trying to declare two character arrays, one after each other. When I do this the arrays seem to overlap onto each other.

#include <iostream>

int main(){
    char cmnd[2] = {'l', 's'};
    char flags[1] = {'a'};
    std::cout << "cmnd array: " << cmnd << std::endl;
    std::cout << "flags array: " << flags << std::endl;
}

The output of this code then becomes:

cmnd array: ls
flags array: als

According to the output my flags array which should only contain {'a'} actually contains {'a','l','s'}.
Why does this occur? Thanks in advance for any insight on this issue.

Aucun commentaire:

Enregistrer un commentaire