何时使用双指针和指针
when to use to double pointers and pointers
// A C program to demonstrate linked list based implementation of queue
#include <stdio.h>
#include <stdlib.h>
struct QNode {
int key;
struct QNode* next;
};
struct Queue {
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue* q, int k)
{
struct QNode* temp = newNode(k);
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q)
{
if (q->front == NULL)
return;
struct QNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d \n", q->front->key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
The above code is from geeksforgeeks website.
in function calls they used pointer to struct,
in function definition they passed pointer to struct.
how it works, I thought we need to use double pointers , otherwise > it is pass by value instead of pass by reference.
the above code works fine, but i have doubt about it.
在 main
中声明了一个变量 q
,它是一个指向结构的指针。变量 q
用作函数参数,这意味着函数接收指向结构的指针。该函数可以取消引用指针并修改结构。变量 q
在技术上是按值传递的,因为它的值是一个指针,这就是函数接收的内容。但是你必须记住 q
指向一个可以被函数修改的结构。
由于这种情况引起了一些混乱,一些人试图引入新的术语,如“通过共享传递”或“对象共享”,以将其与按值传递原始值(如“int”)区分开来。
如果您将指针传递给指针,那么该函数可能会修改 main
中声明的变量 q
并更改它,使其指向一个完全不同的结构。那将是(技术上)通过引用传递,因为您正在传递对变量的引用。
// A C program to demonstrate linked list based implementation of queue
#include <stdio.h>
#include <stdlib.h>
struct QNode {
int key;
struct QNode* next;
};
struct Queue {
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue* q, int k)
{
struct QNode* temp = newNode(k);
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q)
{
if (q->front == NULL)
return;
struct QNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d \n", q->front->key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
The above code is from geeksforgeeks website. in function calls they used pointer to struct, in function definition they passed pointer to struct. how it works, I thought we need to use double pointers , otherwise > it is pass by value instead of pass by reference. the above code works fine, but i have doubt about it.
在 main
中声明了一个变量 q
,它是一个指向结构的指针。变量 q
用作函数参数,这意味着函数接收指向结构的指针。该函数可以取消引用指针并修改结构。变量 q
在技术上是按值传递的,因为它的值是一个指针,这就是函数接收的内容。但是你必须记住 q
指向一个可以被函数修改的结构。
由于这种情况引起了一些混乱,一些人试图引入新的术语,如“通过共享传递”或“对象共享”,以将其与按值传递原始值(如“int”)区分开来。
如果您将指针传递给指针,那么该函数可能会修改 main
中声明的变量 q
并更改它,使其指向一个完全不同的结构。那将是(技术上)通过引用传递,因为您正在传递对变量的引用。