c - 为什么打印结构成员会导致乱码?

标签 c pointers struct

我正在尝试打印和访问结构体的成员,以便使用其中包含的一些数据。

我有两个结构,第一个是二叉搜索树并包含重复的键。我正在尝试提取唯一的 key 并将它们存储在单独的结构中。

注意:我可以从 unique_key 函数中打印 node 结构中的所有唯一键。但是我需要从 main 中访问这些唯一的键。因此我的想法是创建一个单独的结构并将其传递回主函数。

定义两个结构:

/* structure containing duplicate keys */
struct node
{
    int KEY;
    char *command;
    char *duration; /* pointer to a char since it is of unknown size*/
    char *time;
    char *description;
    int count;
    struct node *left, *right;
};

/* structure to hold unique keys*/
 typedef struct {
    int KEY;
    char *command;
    char *duration; /* pointer to a char since it is of unknown size*/
    char *time;
    char *description;
 }unique;

我正在使用实用函数来遍历二叉搜索树。该函数提供了一个指向二叉搜索树*root的指针,并打印所有唯一键。

/* A utility function to find deepest keys of BST */
/* This is done by extracting the key with the lowest count, 
since count member of node struct gets incremented each time it is read. */
unique* unique_key(struct node *root)
{   
    unique *temp = (struct unique *)malloc(sizeof(unique));
    if (root != NULL)
    {
        unique_key(root->left);
        if (root->count == 1) {
            //the printf statement below prints all unique keys
            //Somehow I need to access in the main function, thus my idea was to create a separate struct as explained above
            printf("%d(%d) -> %s %s %s %s \n", root->KEY, root->count, root->command, root->time, root->duration, root->description);
            temp->KEY = root->KEY;
            temp->command = root->command;
            temp->description = root->description;
            temp->duration = root->duration;
        }
        unique_key(root->right);
    }

    return temp;
}

主要驱动代码:

int main()
{
    /* Let us create following BST.  Passing values along with key */
    struct node *root = NULL;
    root = insert_node(root, 12, "C", "1200", "79", "Meeting");
    root = insert_node(root, 3, "C", "1300", "60", "Lunch");
    root = insert_node(root, 2, "C", "1400", "30", "Dinner");
    root = insert_node(root, 1, "C", "0600", "90", "Work");
    root = insert_node(root, 5, "C", "4300", "30", "Diyoor");
    root = insert_node(root, 7, "C", "5608", "30", "Dinner");
    root = insert_node(root, 9, "C", "1409", "35", "t");
    root = insert_node(root, 2, "C", "1600", "60", "play");
    root = insert_node(root, 2, "U", "1800", "88", "eve");

    printf("Inorder traversal of the given tree \n");
    inorder(root);  //prints all keys and subsequent values
    unique *data = NULL;
    data = unique_key(root); //prints only unique keys
    printf("%d %s\n", data[1].KEY, data[1].command); //cannot print keys in main function to access from here on
}

示例输出如下。 BST 已相应填充,并且所有遍历函数都运行良好。

Inorder traversal of the given tree
1(1) 2(3) 2(2) 2(1) 3(1) 5(1) 7(1) 9(1) 12(1)
Deepest unique keys of the given tree
1(1) -> C 0600 90 Work
2(1) -> U 1800 88 eve
3(1) -> C 1300 60 Lunch
5(1) -> C 4300 30 Diyoor
7(1) -> C 5608 30 Dinner
9(1) -> C 1409 35 t
12(1) -> C 1200 79 Meeting


-33686019  å, æ

有时会出现上面未显示的其他乱码。

我的问题是:如何打印和访问 unique 的成员以及为什么我看到乱码?如有任何建议,我们将不胜感激。

<小时/>

编辑:

这些是我试图在unique中保存的唯一键:

1(1) -> C 0600 90 Work
2(1) -> U 1800 88 eve
3(1) -> C 1300 60 Lunch
5(1) -> C 4300 30 Diyoor
7(1) -> C 5608 30 Dinner
9(1) -> C 1409 35 t
12(1) -> C 1200 79 Meeting

我预计 printf("%d %s\n", data[1].KEY, data[1].command); 返回 2 U

最佳答案

目前,您的代码逐步遍历树并能够很好地打印它,但在内部调用 unique_key 时您实际上并没有收集任何结果,并且您的返回类型不足以返回事物列表(列表需要一个结尾,可以是大小变量,也可以是空终止)。您需要更改代码才能实际收集结果。一种方法是使用基本 vector (自扩展数组),如下所示:

struct MyVector {
  void **data;
  size_t head;
  size_t size;
};
MyVector new_MyVector(size_t initial_size)
{
  MyVector list = {
    .data = malloc(sizeof(void*) * initial_size),
    .head = 0,
    .size = initial_size,
  };
  return list;
}
void push_MyVector(MyVector *vec, unique *item)
{
  if (vec->head <= vec->size) {
    vec->data = realloc(*vec->data)
    vec->size *= 2;
  }
  vec->data[vec->head] = item;
  vec->head++;
}

然后像这样使用它

unique* unique_key(struct node *root, MyVector *list) {
  ...
  unique *left = unique_key(root->left);
  push_MyVector(list, left)
  ...
  unique *right = unique_key(root->right);
  push_MyVector(list, right)
  ...
}

一些注意事项:因为 data 是一个双指针,所以要释放 vector ,您需要迭代它并释放每个单独的项目。我选择将 data 设置为双指针,以使您当前的代码基本兼容,但最好将其设置为单指针,并​​直接让您的函数写入 vector 。同样没有实现的是扩展 vector ,这样你就不会用完空间,尽管定义了头部和大小,你可以做到这一点(只需查找如何使用 realloc )。

PS:此代码未经测试,但我希望您能了解总体思路

关于c - 为什么打印结构成员会导致乱码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53354841/

相关文章:

swift - 如何将 Unmanaged<CFTypeRef> 转换为 Swift 3

转换结构指针

c# - 这个 C# 结构有什么问题?

c - ELF 的符号解析

c - 更改指针是否被视为 C 中的原子操作?

c - C for Linux 中的公钥实现

c - 如何在字符串数组上使用二分查找

c - 服务器编程错误

c++ - 从 C++ 中的 int 指针返回只读指针

c - 为什么这样一个结构体包含两个只包含一个元素的数组字段?