[英]pointer to a struct becomes NULL at every function call
問題在於,每次調用addNodePos
函數時, head
指針均為NULL
(在調試器中看到),並且它僅創建一個節點的列表,該節點指向自身,因為它是一個循環的雙向鏈接列表。 並顯示“列表為空”。 因為在傳遞給printList
函數時list
也是NULL
。 一直試圖了解原因,但仍然沒有結果。
這是代碼(根據SSCCE刪除了多余的代碼)
#include <stdio.h>
#include <stdlib.h>
struct DoubleList
{
int id;
struct DoubleList *next;
struct DoubleList *prev;
};
void addNodePos(struct DoubleList* head, int value, int position);
void printList (struct DoubleList* head);
//void clearList (struct DoubleList* head);
int main()
{
int value, position;
struct DoubleList *list = NULL;
printf("\nvalue: ");
scanf("%x", &value);
printf("position: ");
scanf("%d", &position);
addNodePos(list, value, position);
printf("\nvalue: ");
scanf("%x", &value);
printf("position: ");
scanf("%d", &position);
addNodePos(list, value, position);
printList(list);
//clearList(list);
return 0;
}
void addNodePos(struct DoubleList* head, int value, int position)
{
int i;
struct DoubleList *node;
if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
node->id=value;
if (head==NULL) {
// points to itself as it is the only node in a list
node->next=node;
node->prev=node;
head=node;
} else {
struct DoubleList *current=head;
for (i = position; i > 1; i--)
current=current->next;
// reassign pointers -- relink nodes
current->prev->next=node;
node->prev=current->prev;
node->next=current;
current->prev=node;
}
}
printf("Element has been added.\n\n");
}
void printList(struct DoubleList* head)
{
if (head==NULL)
printf("\nList is empty.\n\n");
else {
struct DoubleList *current=head;
printf("\nThe list: ");
do {
printf("%d", current->id);
current=current->next;
if(current != head)
printf("<->");
} while(current!=head);
printf("\n\n");
}
}
head的地址按值傳遞,因此您的更改僅反映在函數本身中。 您必須傳遞一個指向head地址的指針,以便您可以更改值。
int main() {
...
addNodePos(&list, value, position);
...
}
void addNodePos(struct DoubleList** headPtr, int value, int position)
{
struct DoubleList *head = *headPtr;
int i;
struct DoubleList *node;
if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
node->id=value;
if (head==NULL) {
// points to itself as it is the only node in a list
node->next=node;
node->prev=node;
head=node;
} else {
struct DoubleList *current=head;
for (i = position; i > 1; i--)
current=current->next;
// reassign pointers -- relink nodes
current->prev->next=node;
node->prev=current->prev;
node->next=current;
current->prev=node;
}
}
printf("Element has been added.\n\n");
}
在用戶的大力幫助下(共享了一些非常有用的鏈接),我設法解決了這一問題。
解決方案:要修改調用者的內存,請將指針傳遞給該內存。
經過一些更新后,我在這里正確地工作了:
#include <stdio.h>
#include <stdlib.h>
struct DoubleList
{
int id;
struct DoubleList *next;
struct DoubleList *prev;
};
void addNodePos(struct DoubleList** headRef, int value, int position);
void printList (struct DoubleList** headRef);
//void clearList (struct DoubleList** headRef);
int main()
{
int value, position;
struct DoubleList *list = NULL;
printf("\nvalue: ");
scanf("%x", &value);
printf("position: ");
scanf("%d", &position);
addNodePos(&list, value, position);
printf("\nvalue: ");
scanf("%x", &value);
printf("position: ");
scanf("%d", &position);
addNodePos(&list, value, position);
printList(&list);
//clearList(head);
return 0;
}
void addNodePos(struct DoubleList** headRef, int value, int position)
{
int i;
struct DoubleList *node;
if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
node->id=value;
if ( (*headRef)==NULL) {
// points to itself
node->next=node;
node->prev=node;
(*headRef)=node;
} else {
struct DoubleList *current=(*headRef);
for (i = position; i > 1; i--)
current=current->next;
// reassign pointers -- relink nodes
current->prev->next=node;
node->prev=current->prev;
node->next=current;
current->prev=node;
}
}
printf("Element has been added.\n\n");
}
void printList(struct DoubleList** headRef)
{
if ( (*headRef)==NULL)
printf("\nList is empty.\n\n");
else {
struct DoubleList *current=(*headRef);
printf("\nThe list: ");
do {
printf("%x", current->id);
current=current->next;
if(current != (*headRef))
printf("<->");
} while(current!=(*headRef));
printf("\n\n");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.