【发布时间】:2019-04-10 16:07:58
【问题描述】:
我正在尝试用 C 解决我的家庭作业,该作业要求我创建一个函数,将 char 值放在排序链表中的正确位置。
实际上我并没有真正理解函数头在 struct typedef name *struct 和 struct typedef name **struct 之间的区别。
当我查看老师的解决方案时,她使用** 这样做了,但不明白这是什么意思。
void insert_in_sorted_list(Node *lst, char x) {
Node *temp;
while (lst) {
if (x > lst->value && x < lst->next->value) {
temp = (Node*)malloc(sizeof(Node));
temp->value = x;
temp->next = lst->next;
lst->next = temp;
}
else lst = lst->next;
}
}
【问题讨论】:
-
int* = 这是 int 的内存地址 int** = 这是包含 int 内存地址的内存地址
-
当您的函数显示时,您无法更改列表中的第一项(lst),因为您需要更改指针指向的内容,然后您需要指针的地址才能更改它。与
foo(int* n) { *n = 1; }比较,这里只有n指向的东西,而不是指针本身
标签: c struct linked-list