简体   繁体   English

[结构指针a =结构指针b]的作用是什么?

[英]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; 所以我的问题是,当我这样做会发生什么: 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;等于conductor = root;的等效语句conductor = root; ?

conductor=root

makes conductor to point to the same address as node , while 使conductor指向与node相同的地址,而

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. 指针本身是一个8字节的整数(尽管其确切大小取决于计算机),它指向内存中的位置。 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). 最初声明指针时,在分配指针之前,它没有有意义的值(就像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. 您可以要求操作系统(通过malloc)为您提供一些指向的内存,也可以通过为其分配内存地址来指向已经拥有的内存。

Assignment 分配

When you do conductor=root , you are copying the value of root (the memory address , not the memory itself) into conductor . 当您做conductor=root ,您会将root的值(内存地址 ,而不是内存本身)复制到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 . 因此,如果你在哪里重新分配root->item ,价值conductor->item 发生变化

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) . 这种现象称为别名(请参阅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时,您要求操作系统在足以存储您请求的数据量的地方留出一些坚固的内存块。 malloc does this, then returns to you the memory location (pointer) of the first byte in that block. malloc执行此操作,然后向您返回该块中第一个字节的存储位置(指针)。 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. 因此,当您说root = malloc(sizeof(struct node)) ,您将获得一个指向一个足以容纳一个节点结构的新内存块的指针。

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. conductor = malloc(sizeof(struct node)) ,您要预留另一个的内存块,并告诉conductor指向该块。 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. 这种方法的作用是使conductor指向结构root点的完全独立的副本。 If you reassign the value of root->item , conductor->item will remain unchanged. 如果您重新分配root->item的值,那么root->item conductor->item将保持不变。

Note that conductor->item in shorthand for (* conductor).item . 请注意,简写为(* conductor).item 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. 首先,您要取消引用conductor指针以获取存储在内存中的值(导体结构),然后访问item成员。 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 . 当您执行conductor->item=root->item ,您conductor->item=root->item的值复制root->item conductor->item变量中,该变量位于您为conductor分配的新存储块中。 You are not changing anything about the conductor variable itself, just the memory it points to. 您无需更改conductor变量本身的任何内容,只需更改其指向的内存即可。

Linked Lists 链表

In the context of linked lists, you would probably want to do something like root->next = conductor . 在链接列表的上下文中,您可能想要做类似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. 这将使next指向与conductor相同的存储位置,因此您可以引用conductor指向的值。 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. 然后, conductor可以在其next成员中具有指向该节点的另一个指针,依此类推,可以根据需要在列表中包含任意数量的链接。 Note that is is common practice to set the next value of the last link to NULL , so you know where the end is. 请注意,通常的做法是将最后一个链接的next值设置为NULL ,这样您就知道结尾在哪里。

its completely pointer concept .. i hope there is nothing to deal with Linkedlist. 它的完全指针概念..我希望没有什么要处理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. 在这里,您正在为导体分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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