当前位置:  开发笔记 > 编程语言 > 正文

C中的*和含义是什么?

如何解决《C中的*和含义是什么?》经验,为你挑选了1个好方法。

我是树木概念的新手.我正在学习SerializationdeSerialization.我从链接获得了一个示例程序,复制并执行它.它跑了,但当我试图理解它时,我无法理解一条线 - void deSerialize(Node *&root, FILE *fp)

为什么*&意思?

整个代码是:

#include 
#define MARKER -1

/* A binary tree Node has key, pointer to left and right children */
struct Node
{
int key;
struct Node* left, *right;
};

/* Helper function that allocates a new Node with the
given key and NULL left and right pointers. */
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}

// This function stores a tree in a file pointed by fp
void serialize(Node *root, FILE *fp)
{
// If current node is NULL, store marker
if (root == NULL)
{
    fprintf(fp, "%d ", MARKER);
    return;
}

// Else, store current node and recur for its children
fprintf(fp, "%d ", root->key);
serialize(root->left, fp);
serialize(root->right, fp);
}

// This function constructs a tree from a file pointed by 'fp'
void deSerialize(Node *&root, FILE *fp)
{
// Read next item from file. If theere are no more items or next
// item is marker, then return
int val;
if ( !fscanf(fp, "%d ", &val) || val == MARKER)
   return;

// Else create node with this item and recur for children
root = newNode(val);
deSerialize(root->left, fp);
deSerialize(root->right, fp);
}

// A simple inorder traversal used for testing the constructed tree
void inorder(Node *root)
{
if (root)
{
    inorder(root->left);
    printf("%d ", root->key);
    inorder(root->right);
}
}

/* Driver program to test above functions*/
int main()
{
// Let us construct a tree shown in the above figure
struct Node *root        = newNode(20);
root->left               = newNode(8);
root->right              = newNode(22);
root->left->left         = newNode(4);
root->left->right        = newNode(12);
root->left->right->left  = newNode(10);
root->left->right->right = newNode(14);

// Let us open a file and serialize the tree into the file
FILE *fp = fopen("tree.txt", "w");
if (fp == NULL)
{
    puts("Could not open file");
    return 0;
}
serialize(root, fp);
fclose(fp);

// Let us deserialize the storeed tree into root1
Node *root1 = NULL;
fp = fopen("tree.txt", "r");
deSerialize(root1, fp);

printf("Inorder Traversal of the tree constructed from file:\n");
inorder(root1);

return 0;
}

任何帮助赞赏.



1> Igal S...:

*&不是一个单一的符号.但两者结合:

*一个指针. &以供参考.

所以你有这个功能:

void deSerialize(Node *&root, FILE *fp)

此函数的第一个参数是对Node指针的引用.

含义 - 使用它时,你发送一个Node *对象.函数本身可以更改此指针值 - 因为您通过引用传递它.

这允许您在函数内部分配内存.

编写此函数的另一种方法是:

Node *deSerialize(Node *root, FILE *fp)

并以不同的方式使用它:

root->left = deSerialize(root->left, fp)

请在此处查看完整解决方案:http://ideone.com/5GzAyd.相关部分:

Node *deSerialize(Node *root, FILE *fp)
{
    // Read next item from file. If theere are no more items or next
    // item is marker, then return
    int val;
    if ( !fscanf(fp, "%d ", &val) || val == MARKER)
       return NULL;

    // Else create node with this item and recur for children
    root = newNode(val);
    root->left = deSerialize(root->left, fp);
    root->right = deSerialize(root->right, fp);
    return root;
}

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有