简体   繁体   中英

What does [Structure pointer a = Structure pointer b] do?

I've only recently exposed myself to Linked Lists and i'm quite hopeless. So anyway,i've given pieces of code below to make myself understood better.

struct node
{
    int item;
    struct node *next;
};
struct node *root;
struct node *conductor;

root = malloc(sizeof(struct node));

root->item = 777;
root->next = 0;

So my question is, What happens when i do: conductor = node; ? Moreover, is there a difference between:

conductor = node; 

and

conductor = malloc(sizeof(struct node));
conductor->item = root->item;
conductor->next = root->next;

If there is a difference, what is the difference? and how would you go about to write an equivalent statement of conductor = root; ?

conductor=root

makes conductor to point to the same address as node , while

conductor=malloc(sizeof(struct node));
conductor->item=root->item;
conductor->next=root->next;

creates a brand new copy. You could try

printf("%p %p", conductor, root);

to see the addresses they point to, or

node->item = 12345;
printf("%d %d", conductor->item, root->item);

to see if they actually refer to the same instance.

Basically, a pointer references an area in memory in which values are stored. A pointer itself is a 8-byte integer (though the exact size is machine-dependent) that refers to a location in memory. When we have a pointer to some type, the value of the pointer refers to the first byte of that type in memory.

When you initially declare a pointer, it has no meaningful value before you assign it (like all variables in C). But to be useful, it needs to know where to point. You can either ask the operating system to give you some memory to point to (via malloc), or you can point to something you already have by assigning it a memory address.

Assignment

When you do conductor=root , you are copying the value of root (the memory address , not the memory itself) into conductor . Since they both have the value, they refer to the same location in memory, so they both point to the same place!

This method makes a copy of the pointer , not the structure , so they both point to the same thing in memory. Thus, if you where to reassign root->item , the value of conductor->item would also change .

Note that you have now given the same memory location two different names, which could be either very convenient or very frustrating. This phenomenon is known as aliasing (see Wikipedia) . Use it with care!

Malloc

When you call malloc, you ask the operating system to set aside some solid block of memory somewhere big enough to store the amount of data you request. malloc does this, then returns to you the memory location (pointer) of the first byte in that block. Thus, when you say root = malloc(sizeof(struct node)) , you get back a pointer to a new block of memory large enough for one node structure.

When you do conductor = malloc(sizeof(struct node)) , you are setting aside another, new block of memory and telling conductor to point to that block. So in the first example, they're looking at the same place, and in the second example, they're looking at different places.

This method has the effect of making conductor point to a totally separate copy of the structure root points to. If you reassign the value of root->item , conductor->item will remain unchanged.

Note that conductor->item in shorthand for (* conductor).item . You first are dereferencing the conductor pointer to get the value stored in memory there (the conductor structure), and then accessing the item member. When you do conductor->item=root->item , you are copying the value of root->item into the conductor->item variable, which is in the new memory block you malloc'd for conductor . You are not changing anything about the conductor variable itself, just the memory it points to.

Linked Lists

In the context of linked lists, you would probably want to do something like root->next = conductor . This will make next point to the same memory location that conductor does, and therefore you can reference the values that conductor points to. conductor could then have another pointer to node referenced in its next member, and so on for as many links in the list as you like. Note that is is common practice to set the next value of the last link to NULL , so you know where the end is.

its completely pointer concept .. i hope there is nothing to deal with Linkedlist.

conductor = node ;

above line is a pointer assignment. after this both pointers will pointing/refer same memory location.

conductor = malloc(sizeof(struct node));
conductor->item = root->item;
conductor->next = root->next;

here you are allocating memory to conductor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM