i have been trying to write the order in which the tree is traversed, to a .txt file in C. Instead of using arrays, i'm using my own data type vector to store the node values of the tree. I then try to create a character array from the items in the vector. This process goes as expected, however when i print the char array to the screen, it prints 5 random character in the beginning. I'm sure enough that the problem isn't in the vector but elsewhere because when i try to print the characters inside the loop, it doesn't print any unexpected characters to the screen (please see image below). Help in spotting the bug will be highly appreciated.
NODE* char_tree = optimised_tree(); // First Node of the tree
vector* tree_items = new_vector();
parse_tree(char_tree, tree_items); // parses the tree and add nodes to vector
int len = vec_size(tree_items); // size of the vector
char xs[len + 2];
char x[2];
for (int i = 0; i < len; ++i)
{
int o = vec_get(tree_items, i); // item at index i in vector tree_items
printf("%c ", o);
x[0] = (char)o;
x[1] = '\0';
strcat(xs, x);
}
xs[len] = '\n';
xs[len + 1] = '\0';
printf("\n-> %sSIZE %lu\n", xs, sizeof(xs));