gpt4 book ai didi

c - 相同地址不同值

转载 作者:行者123 更新时间:2023-11-30 15:19:35 25 4
gpt4 key购买 nike

我正在尝试从链接列表中创建多个数组。因此,一个链表列表收集了其他链表头。

但是,当我放置链表头地址时放入 int 变量中,然后将 int 变量放入返回到指针。

指针持有相同的地址,但指针的值不同
例如)

&(list.head) : 0x0032FAFAC
*(list.head) : 10
pointer : 0x0032FAFAC
*pointer : 1530784

我已经删除了不必要的代码。程序是

  1. TotalList 添加列表头。

  2. 前往插入功能。

  3. 转到 Print2 函数并将列表头地址赋予指针。

  4. 指针的值与之前的值不同。

我会等待你的答复。谢谢。

#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

最佳答案

&a显示a的地址

*a显示查看地址a时的值

所以在这种情况下:

 &(list.head) == pointer
(list.head) == *pointer
*(list.head) == **pointer

关于c - 相同地址不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30572964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com