【发布时间】:2015-06-01 12:00:44
【问题描述】:
我正在尝试从链表中创建一个多重数组。 因此一个链表会收集其他链表的头部。
然而,当我把链表头地址 放入 int 变量,然后放入 int 变量 回到指针。
指针的地址相同,但指针的值不同
例如)
&(list.head) : 0x0032FAFAC
*(list.head) : 10
pointer : 0x0032FAFAC
*pointer : 1530784
我已经删除了不必要的代码。 程序是
TotalList 添加列表头。
进入插入功能。
转到 Print2 函数并将列表头地址赋予一个指针。
指针的值与前一个不同。
我会等待你的答复。谢谢。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node* next;
};
struct Linkedlist
{
struct node* head;
int (*Search)(struct node** head, struct node** pNode, struct node** nNode);
int (*Insert)(struct node** head, int data);
int (*Delete)(struct node** head);
int (*Print)(struct node** head);
int (*Print2)(struct node** head);
int (*Move)(struct node** head);
};
int Insert(struct node** head, int data)
{
struct node* newNode;
struct node* temp;
struct node* temp2;
newNode =(struct node*)malloc(sizeof(struct node));
newNode->key=data;
printf("address in int variable (Insert function) : %p\n",data);
newNode->next=0;
if((*head)==NULL)
{
(*head)=newNode;
return 1;
}
else if(Search(head,&temp,&temp2)==1)
{
temp->next=newNode;
return 1;
}
else
return 0;
}
int Print2(struct node** head)
{
struct node* temp = (*head);
int* kp;
while(temp!=NULL)
{
printf("Linked list key : %p\n",temp->key);
kp=temp->key;
printf("value of pointer with Linked list key : %d \n",*kp);
//tail을 찾았을 경우
if(temp->next==NULL)
{
printf("\n");
return 1;
}
temp=temp->next;
}
return 0;
}
int main()
{
struct Linkedlist list;
struct Linkedlist list2;
struct Linkedlist list3;
struct Linkedlist TotalList;
Linkedlist_init(&list);
Linkedlist_init(&list2);
Linkedlist_init(&list3);
Linkedlist_init(&TotalList);
list.Insert(&(list.head),10);
list.Insert(&(list.head),20);
list.Insert(&(list.head),30);
list.Insert(&(list2.head),40);
list.Insert(&(list2.head),50);
list.Insert(&(list2.head),60);
list.Insert(&(list3.head),70);
list.Insert(&(list3.head),80);
list.Insert(&(list3.head),90);
printf("&(list.head) : %p\n",&(list.head));
printf("&(list2.head) : %p\n",&(list2.head));
printf("&(list3.head) : %p\n",&(list3.head));
printf("*(list.head) : %d\n",*(list.head));
printf("*(list2.head) : %d\n",*(list2.head));
printf("*(list3.head) : %d\n",*(list3.head));
TotalList.Insert(&(TotalList.head),&(list.head));
TotalList.Insert(&(TotalList.head),&(list2.head));
TotalList.Insert(&(TotalList.head),&(list3.head));
list.Print(&(list.head));
list.Print(&(list2.head));
list.Print(&(list3.head));
TotalList.Print2(&(TotalList.head));
//printf("%d\n",list.Delete(&(list.head)));
//list.Print(&(list.head));
return 0;
}
结果:
&(list.head) : 003DFE34
&(list2.head) : 003DFE10
&(list3.head) : 003DFDEC
*(list.head) : 10
*(list.head) : 40
*(list.head) : 70
address in int variable (Insert function) : 003DFE34
address in int variable (Insert function) : 003DFE10
address in int variable (Insert function) : 003DFDEC
10 20 30
40 50 60
70 80 90
Linked list key : 003DFE34
value of pointer with Linked list key : 2117552
Linked list key : 003DFE10
value of pointer with Linked list key : 2104160
Linked list key : 003DFDEC
value of pointer with Linked list key : 2104328
【问题讨论】:
-
代码是c还是c++?
-
我们真的需要两 (2) 行这么多代码吗!表达式?抱歉,我没有尝试在此处捕获您的代码...
-
如果是 C,不要转换
malloc的结果,如果是 C++,你应该使用new代替(真的,应该使用智能指针或容器类,但是。 ..) 的malloc并在声明变量时删除额外的struct(因为 C++ 自动将struct设为类型名)。 -
如果
pointer是&list.head,那么*pointer是list.head,而不是*list.head(即**pointer)。 -
附带说明,请注意编译器警告。对于您的 printf 说明符:
printf("... %p\n",data);。%p用于指针,data是int。