简体   繁体   English

结构指针和箭头运算符如何工作?

[英]How structure pointer and arrow operator works?

#include <stdio.h> 
#include <stdlib.h> 

str1 
{ 
     int k; 
     struct str1 * ptr1; 
}; 

int main(void) 
{ 
         int * p1, * p2; 
         struct str1 * ptr2; 

         ptr2 = (struct str1 *) malloc(sizeof(struct str1)); 
         ptr2 -> ptr1 = (struct str1 *) malloc(sizeof(struct str1)); 
         ptr2 -> ptr1 -> ptr1 = ptr2; 
         ptr2 -> k = 7; 
         ptr2 -> ptr1 -> k = 25; 
         p1 = (int *) ptr2 -> ptr1; 
         p2 = (int *) ptr2 -> ptr1 -> ptr1;  
         printf("%d  %d\n", * p1, * p2); 

         ptr2 -> ptr1 -> ptr1 -> k ++; 
         printf("%d  %d\n", * p1, * p2); 

         ptr2 -> ptr1 -> ptr1 = ptr2 -> ptr1; 
         ptr2 -> ptr1 -> ptr1 -> k = 44; 
         printf("%d  %d\n", * p1, * p2); 

         * p1 = * p2 = 65; 
         if( ptr2 = = ptr2 -> ptr1 )  printf("Equal\n"); 
         else  printf("Not equal\n"); 
         return 0;  
} 

In this code, I did not understand what this ptr2 -> ptr1 -> ptr1 = ptr2;在这段代码中,我不明白这个ptr2 -> ptr1 -> ptr1 = ptr2; code works.代码有效。 Is it attaching a structure another structure and ptr2 -> ptr1 -> ptr1 = ptr2;它是否附加一个结构另一个结构和ptr2 -> ptr1 -> ptr1 = ptr2; What ths code pointing at.代码指向什么。 Sorry for my bad english.对不起,我的英语不好。 Thank you for your answers.谢谢您的回答。

The str1 struct contains a pointer to another structure of type str1 . str1 struct包含指向另一个str1类型结构的指针。 So it is a recursive function, and you can dereference infinitely (or until you reach a str1 struct that has its pointer equal to NULL.所以它是一个递归函数,你可以无限地取消引用(或者直到你到达一个指针等于 NULL 的str1 struct
So ptr2 -> ptr1 -> ptr1 = ptr2;所以ptr2 -> ptr1 -> ptr1 = ptr2; is assigning the current structure of ptr2 to (recursively going up to the last) the last structure.正在将ptr2的当前结构分配给(递归ptr2升到最后一个)最后一个结构。 At that point that structure's own pointer is NULL, so you cannot dereference further.此时,该结构自身的指针为 NULL,因此您无法进一步取消引用。

First of all the the structure str1 declaration is invalid as it misses the keyword struct which introduces a structure declaration.首先,结构str1声明是无效的,因为它错过了引入结构声明的关键字struct I am assuming it's a typo.我假设这是一个错字。 The other typo is in equal to operator in this statement此语句中的另一个错字等于运算符

if( ptr2 = = ptr2 -> ptr1 )  printf("Equal\n"); 
         ^^^

it should be == .它应该是==

The structure str1 is a self-referential 1) structure as the ptr1 member is pointer to itself ie struct str1 .结构str1是一个自引用1)结构,因为ptr1成员是指向自身的指针,即struct str1

This statement这个说法

         ptr2 = (struct str1 *) malloc(sizeof(struct str1)); 

allocates memory for struct str1 type (assuming malloc is success) and the pointer returned will be assigned to ptr2 .struct str1类型分配内存(假设malloc成功),返回的指针将分配给ptr2

The in-memory view would be something like this:内存中的视图将是这样的:

ptr2---+
       |
       +-------+
       |   |   | 
       +-------+
         k  ptr1

The next statement下一个声明

         ptr2 -> ptr1 = (struct str1 *) malloc(sizeof(struct str1)); 

does same as what its previous statement does ie allocates memory for struct str1 type.与之前的语句相同,即为struct str1类型分配内存。 The allocated memory reference will be assigned to pointer ptr2 -> ptr1 .分配的内存引用将分配给指针ptr2 -> ptr1

The in-memory view would be something like this:内存中的视图将是这样的:

ptr2---+
       |
       +-------+          +-------+
       |   |   |--------->|   |   |
       +-------+          +-------+
         k  ptr1            k  ptr1

The next statement下一个声明

ptr2 -> ptr1 -> ptr1 = ptr2; 

will assign the pointer ptr2 to ptr2 -> ptr1 -> ptr1 .将指针ptr2分配给ptr2 -> ptr1 -> ptr1 That means, after execution of this statement, both pointers ptr2 and ptr2 -> ptr1 -> ptr1 will point to same memory address and a loop will be created.这意味着,在执行此语句后,指针ptr2ptr2 -> ptr1 -> ptr1都将指向相同的内存地址,并会创建一个循环。

The in-memory view would be:内存视图将是:

ptr2---+ +-----------------------------+
       | |                             |
       +-------+          +-------+    |
       |   |   |--------->|   |   |----+
       +-------+          +-------+
         k  ptr1            k  ptr1

Note that this statement注意这个说法

         ptr2 -> ptr1 -> ptr1 = ptr2 -> ptr1; 

will make pointer ptr2 -> ptr1 -> ptr1 point to structure which it is member of.将使指针ptr2 -> ptr1 -> ptr1指向它所属的结构。 The in-memory view would be:内存视图将是:

ptr2---+                   +-----------+
       |                   |           |
       +-------+          +-------+    |
       |   |   |--------->|   |   |----+
       +-------+          +-------+
         k  ptr1            k  ptr1

Hope this clarifies your doubt.希望这能澄清您的疑问。 Let me know if you have any further question or confusion.如果您有任何进一步的问题或困惑,请告诉我。


1). 1)。 A self-referential structure is a structure in which one of its members is a pointer to the structure itself.自引用结构是一种结构,其中其成员之一是指向结构本身的指针。 It's commonly used in data structures like Linked List, Tree etc. implementation.它常用于链表、树等数据结构的实现。

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

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