[英]How to pass a struct by reference?
I can't figure it out, this is what I have so far. 我无法弄清楚,这就是我到目前为止所拥有的。
But the function Enqueue()
is not changing the Head
& Tail
objects from the main method. 但是函数
Enqueue()
不会从main方法更改Head
& Tail
对象。
How do I do this? 我该怎么做呢?
#include <stdio.h>
typedef struct queuenode{
int data;
struct queuenode *next;
} Queue_type;
void Enqueue( Queue_type *head, Queue_type *tail , int item)
{
Queue_type *temp;
temp->data = item;
temp->next = NULL;
if (tail == NULL){//if first node
head=temp;
tail=temp;
}
else{
tail->next = temp;
tail = temp;
}
}
void main(void)
{
Queue_type *Head;
Queue_type *Tail;
Head = NULL;
Tail = NULL;
printf("\n[*]Enter Number to Enqueue : ");
scanf("%d", &item);
Enqueue(Head, Tail, item);
}
How to pass a pointer by reference? 如何通过引用传递指针? Pass it by reference:
通过引用传递它:
void Enqueue( Queue_type *&head, Queue_type *&tail , int item)
Other problems: 其他问题:
Queue_type *temp;
temp->data = item;
is undefined behavior. 是未定义的行为。
Mandatory link. 强制链接。 You're writing C code in a C++ environment.
您正在C ++环境中编写C代码。 Just don't.
只是不要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.