Reputation: 433
I have this code. The problem is, that when I'm trying to display the value stored using union, it displays just some random numbers. Can you help me, please?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef union s_datatype {
int t_int;
char* t_char;
double t_double;
bool t_bool;
} t_datatype;
typedef struct s_node {
char*name;
int type;
t_datatype value;
struct s_node*left;
struct s_node*right;
} t_node;
t_node*init_node (char*name, int type, t_datatype value) {
t_node*root = malloc(sizeof(t_node));
if (root == NULL) {
return NULL;
}
root->name = name;
root->type = type;
root->value = value;
root->left = NULL;
root->right = NULL;
return root;
}
void init_root (t_node*(*root)) {
*root = NULL;
}
void insert_node (t_node*(*root), t_node*node) {
if ((*root) == NULL) {
(*root) = node;
} else {
if (strcmp((*root)->name, node->name) > 0) {
insert_node(&((*root)->left), node);
} else if (strcmp((*root)->name, node->name) < 0) {
insert_node(&((*root)->right), node);
} else if (strcmp((*root)->name, node->name) == 0) {
(*root) = node;
}
}
}
void free_table (t_node*node) {
if (node != NULL) {
free_table(node->left);
free_table(node->right);
free (node);
node = NULL;
}
}
void print_tree2(t_node*root, char* sufix, char fromdir) {
if (root != NULL) {
char* suf2 = (char*) malloc(strlen(sufix) + 4);
strcpy(suf2, sufix);
if (fromdir == 'L') {
suf2 = strcat(suf2, " |");
printf("%s\n", suf2);
} else {
suf2 = strcat(suf2, " ");
}
print_tree2(root->right, suf2, 'R');
printf("%s +-[%s,%d,%d]\n", sufix, root->name, root->value.t_int);
strcpy(suf2, sufix);
if (fromdir == 'R') {
suf2 = strcat(suf2, " |");
} else {
suf2 = strcat(suf2, " ");
}
print_tree2(root->left, suf2, 'L');
if (fromdir == 'R') {
printf("%s\n", suf2);
}
free(suf2);
}
}
void print_tree(t_node*root)
{
printf("Binary tree structure:\n");
printf("\n");
if (root != NULL)
print_tree2(root, "", 'X');
else
printf("Tree is empty\n");
printf("\n");
printf("=================================================\n");
}
int main (void) {
t_node*root;
init_root(&root);
t_datatype tt;
tt.t_int = 0;
t_node*node = init_node("zero", 0, tt);
insert_node(&root, node);
tt.t_int = 1;
node = init_node("first", 1, tt);
insert_node(&root, node);
tt.t_int = 2;
node = init_node("second", 2, tt);
insert_node(&root, node);
tt.t_int = 3;
node = init_node("third", 3, tt);
insert_node(&root, node);
tt.t_int = 4;
node = init_node("fourth", 4, tt);
insert_node(&root, node);
tt.t_int = 5;
node = init_node("fifth", 5, tt);
insert_node(&root, node);
tt.t_int = 6;
node = init_node("sixth", 6, tt);
insert_node(&root, node);
tt.t_int = 7;
node = init_node("seventh", 7, tt);
insert_node(&root, node);
tt.t_int = 8;
node = init_node("eighth", 8, tt);
insert_node(&root, node);
tt.t_int = 9;
node = init_node("ninth", 9, tt);
insert_node(&root, node);
tt.t_int = 10;
node = init_node("tenth", 10, tt);
insert_node(&root, node);
print_tree(root);
free_table(root);
return 0;
}
I'm getting a warning - Format expects a matching int argument. (The line, where I'm displaying the values in the print_tree2
)
The output:
Binary tree structure:
+-[zero,0,1]
|
| +-[third,3,10]
| | |
| | | +-[tenth,10,16]
| | | |
| | +-[sixth,6,18]
| | |
| | +-[seventh,7,18]
| |
| +-[second,2,12]
| | |
| | | +-[ninth,9,13]
| | | |
| | +-[fourth,4,15]
| |
+-[first,1,9]
|
+-[fifth,5,9]
|
+-[eighth,8,12]
=================================================
Upvotes: 2
Views: 112
Reputation: 1937
gcc warning:
main.c:72:31: warning: more '%' conversions than data arguments [-Wformat]
printf("%s +-[%s,%d,%d]\n", sufix, root->name, root->value.t_int);
I think you wanted
printf("%s +-[%s,%d,%d]\n", sufix, root->name, root->type, root->value.t_int);
Upvotes: 3