양수현
양수현

Reputation: 11

Same address with different value

I'm trying to make a multiple array out of linked lists. Therefore one linked list list collects other linked lists head.

However, when i put the linked lists head address into a int variable and then puts the int variable back to a pointer.

The pointer holds the same address however the value to pointer is different
ex)

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

I've deleted the codes that is unnecessary. The procedure is

  1. TotalList adds list heads.

  2. Going to Insert function.

  3. Going to Print2 function and giving the list head address to a pointer.

  4. value of pointer is different compared to the previous one.

I'd be waiting for your answer. Thank you.

#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;
}

Result :

&(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

Upvotes: 0

Views: 1537

Answers (2)

Kami Kaze
Kami Kaze

Reputation: 2080

&a shows the address of a

*a shows the value when you look at the address a

so in this case:

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

Upvotes: 1

mtijanic
mtijanic

Reputation: 2902

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

You are missing an indirection step here. (list.head) is also a pointer, with the value of 1530784.

So, &(list.head) is the address of the pointer list.head. It is always the same. (list.head) is the value of that pointer. It can change, but it doesn't in your program. *(list.head) is the value of the object pointed to, which is 10.

Upvotes: 0

Related Questions