[英]Why does printing struct members result in gibberish?
我正在嘗試打印和訪問結構的成員,以便使用其中包含的一些數據。
我有兩個結構,第一個是二叉搜索樹並且包含重復的鍵。 我正在嘗試提取唯一鍵並將它們存儲在單獨的結構中。
注意:我可以從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
。
在 C 數組中,索引從 0 開始,而不是 1。通過嘗試訪問data[1]
您正在訪問數據之后的垃圾。 printf 需要使用data[0]
而不是data[1]
。
目前,您的代碼逐步遍歷樹並能夠很好地打印它,但是在內部調用 unique_key 時實際上並沒有收集任何結果,並且您的返回類型不足以返回事物列表(列表需要一個結尾,要么是一個大小變量,要么是一個空終止)。 您需要更改代碼以實際收集結果。 一種方法是使用基本向量(自擴展數組),如下所示:
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 是一個雙指針,要釋放向量,您需要遍歷它並釋放每個單獨的項目。 我選擇將 data 設為雙指針以使您當前的代碼大部分兼容,但最好將其設為單指針,並直接讓您的函數寫入向量。 同樣沒有實現的是擴展向量,這樣你就不會用完空間,盡管定義了頭部和大小,你可以這樣做(只需查看如何使用 realloc )。
PS:這段代碼都是未經測試的,但我希望你能大致了解
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.