c - 使用 union 存储的值显示不正确

标签 c unions

我有这个代码。问题是,当我尝试显示使用 union 存储的值时,它只显示一些随机数。你能帮我一下吗?

#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;
}

我收到警告 - 格式需要匹配的 int 参数。 (我在 print_tree2 中显示值的行)

输出:

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]

=================================================

最佳答案

海湾合作委员会警告:

main.c:72:31: warning: more '%' conversions than data arguments [-Wformat]
        printf("%s  +-[%s,%d,%d]\n", sufix, root->name, root->value.t_int);

我想你想要

printf("%s  +-[%s,%d,%d]\n", sufix, root->name, root->type, root->value.t_int);

关于c - 使用 union 存储的值显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20365995/

相关文章:

c - c中的结构和标记 union

c++ - 什么加密随机生成器 C 代码可以在 WP8.1 上使用?

c - 如何在 C 中正确释放 char**

c - 在c中将int存储为字符串的有效方法

使用多个 main() 进行编译

c++ - 标记 union 内字符串影响的段错误

c - 确定 C 程序的最简单方法

c++ - 标记 union C++

c - 当我为内部有 union 的嵌套结构分配和释放内存时出了什么问题

C++: union 与按位运算符