【问题标题】:Same address with different value相同地址不同值
【发布时间】:2015-06-01 12:00:44
【问题描述】:

我正在尝试从链表中创建一个多重数组。 因此一个链表会收集其他链表的头部。

然而,当我把链表头地址 放入 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

【问题讨论】:

  • 代码是c还是c++?
  • 我们真的需要两 (2) 行这么多代码吗!表达式?抱歉,我没有尝试在此处捕获您的代码...
  • 如果是 C,不要转换 malloc 的结果,如果是 C++,你应该使用 new 代替(真的,应该使用智能指针或容器类,但是。 ..) 的 malloc 并在声明变量时删除额外的 struct(因为 C++ 自动将 struct 设为类型名)。
  • 如果pointer&amp;list.head,那么*pointerlist.head,而不是*list.head(即**pointer)。
  • 附带说明,请注意编译器警告。对于您的 printf 说明符:printf("... %p\n",data);%p 用于指针,dataint

标签: c pointers


【解决方案1】:

&a 显示a的地址

*a 显示地址 a 时的值

所以在这种情况下:

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

【讨论】:

    【解决方案2】:
    &(list.head) : 0x0032FAFAC
    *(list.head) : 10
    pointer : 0x0032FAFAC
    *pointer : 1530784
    

    您在这里缺少一个间接步骤。 (list.head)也是一个指针,其值为1530784

    所以,&amp;(list.head) 是指针list.head 的地址。它总是一样的。 (list.head) 是该指针的值。它可以改变,但它不会在你的程序中。 *(list.head)是指向的对象的值,即10。

    【讨论】:

    • 主要问题是当我将 &(list.head) 的地址放入 int 变量 (A) 中,然后将其放入新指针 ex) int* ptr=A; printf("%d",*ptr) 指向不同的值
    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多