[英]What does *& mean in C?
I was new to the concepts of trees.我对树的概念很陌生。 I was studying
Serialization
and deSerialization
.我正在研究
Serialization
和deSerialization
序列化。 I got a example program from a link, copied it, and executed it.我从链接中获得了一个示例程序,复制了它并执行了它。 It ran, but when I tried to understand it, I could not understand one line -
void deSerialize(Node *&root, FILE *fp)
它运行了,但是当我试图理解它时,我无法理解一行 -
void deSerialize(Node *&root, FILE *fp)
Why is *&
mean?为什么
*&
是什么意思?
The whole code is:整个代码是:
#include <stdio.h>
#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;
}
Any help appreciated.任何帮助表示赞赏。
The *&
is not a single symbol. *&
不是单个符号。 But a combination of two:但是两者的结合:
*
for a pointer. *
为指针。 &
for reference. &
供参考。
So you have the function:所以你有这个功能:
void deSerialize(Node *&root, FILE *fp)
This function first parameter is a reference to a Node pointer.这个函数的第一个参数是一个节点指针的引用。
Meaning - when using it you send it a Node *
object.含义 - 使用它时,您向它发送一个
Node *
对象。 And the function itself can change this pointer value - since you passed it by reference.并且函数本身可以更改此指针值 - 因为您通过引用传递了它。
This allows you to allocate memory inside the function.这允许您在函数内部分配内存。
A different way to write this function would be:编写此函数的另一种方法是:
Node *deSerialize(Node *root, FILE *fp)
And use it differently ofcourse:当然也可以不同地使用它:
root->left = deSerialize(root->left, fp)
See full solution here: http://ideone.com/5GzAyd .在此处查看完整的解决方案:http: //ideone.com/5GzAyd 。 And relevant part:
和相关部分:
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;
}
In C?
在 C?
It means nothing and will give you syntax error.它没有任何意义,会给你语法错误。
In C++?
在 C++ 中?
It is a reference to the pointer.它是对指针的引用。
Node *&root.节点 *&root. So its
所以是
Node* which is the Node pointer
& root where root is reference variable
Note that this is in C++ and not C.请注意,这是在 C++ 中,而不是在 C 中。
Also you can check a related thread: What does *&
in a function declaration mean?您还可以查看相关线程: 函数声明中的
*&
是什么意思?
You can read it as你可以把它读成
Node* &root
It is simply a reference to a pointer variable.它只是对指针变量的引用。 You need it because there may be a case where your root is empty and you need to change its value.
您需要它,因为在某些情况下您的根目录为空,您需要更改它的值。
You can achieve it by using pointer to pointer too ie.您也可以通过使用指向指针的指针来实现它,即。
Node **root
But there are advantages with references-但参考文献也有优势——
Once a reference is initialized to refer to an object, it can not be changed to refer to another object (with pointer you can always).一旦引用被初始化为引用一个对象,就不能将其更改为引用另一个对象(使用指针总是可以的)。 So, it ensures that you will not change your root to point somewhere else accidently , which is possible if you use pointer to pointer.
因此,它可以确保您不会意外地将根目录更改为指向其他地方,如果您使用指向指针的指针,这是可能的。
I have simple way to understand this我有简单的方法来理解这一点
1)we know 1)我们知道
int *ptr
this means ptr is pointer to int variable这意味着 ptr 是指向 int 变量的指针
2)we also know 2)我们也知道
int &ref
this means ref is reference to int variable这意味着 ref 是对 int 变量的引用
now we replace int in 2) with int *现在我们用 int * 替换 2) 中的 int
int* &X
that means X is reference of pointer and pointer points to int variable这意味着 X 是指针的引用,并且指针指向 int 变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.