In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to link two lonely structural variables in C language". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The first thing we need to know is that structure pointers can be used as internal member variables of a structure type. Suppose we have a student_t structure type that can contain a structure variable defined by itself. As shown in figure 1.
Figure 1 when defining a structure, add a structure pointer of its own type
As shown in figure 1, we can retain the tag option for the structure when we use typedef to define the structure storage type. This tag can be defined in conjunction with struct when defining pointers to structures of this type. Well, we have now defined a structure of type student_t. One of the most striking features of this structure is that its member variables contain a pointer to its own (student_t) type. Here's a hypothesis. Assuming that Xiao Ming and Xiao Li are students in the same class, we can define two structural variables to store their names and ages, as shown in figure 2.
Figure 2 print the information of Xiao Ming and Xiao Li
Now we can think about it again, if we want to establish a relationship, that is, the classmate sitting behind Xiao Ming, her name is Xiao Li, in other words, whether we can find a relationship and find Xiao Li's information through Xiao Ming. The answer is yes. We can see that the structure used to describe Xiao Ming also contains a structure pointer * next. Because the structure types used to describe Xiao Ming and Xiao Li are exactly the same, * next pointer can point to Xiao Li structure. As shown in figure 3.
Fig. 3 next structure pointer points to another structure variable
The pointing operation of this pointer can be implemented in code by assigning the first address of the XiaoLi structure to the Xiaoming.next. After doing this, the next pointer of Xiaoming points to XiaoLi, and we can easily query the relevant information of XiaoLi through Xiaoming. It should be noted here that Xiaoming.next is a structure pointer, so when you use the Xiaoming structure to query XiaoLi information, you need to use the point operation of Xiaoming.next, that is, Xiaoming.next- > name, and Xiaoming.next- > age. The specific code is shown in figure 4.
Fig. 4 print the information of a structure variable using next pointer
Now let's expand this information a little bit, assuming that two more buddies, called MaLi and JieKe, have been added to the group Xiaoming and XiaoLi. Then we can also use the same way to store MaLi and JieKe information, and use next pointers to "concatenate" their information one by one. As shown in figure 5.
Figure 5 use Xiaoming to print out the information of all the students
The above printf function is still troublesome to write the program. We need to write four different printf functions for these four students. If the number of students reaches 100, we also need to write 100 printf? So is there any way to print out the information of the whole connected structure just by writing a printf? The answer is yes again, let's take a look at the relationship between these structures. As shown in figure 6.
Fig. 6 the relationship between the four structures
If we want to be able to print out all the information about these four or more structures with only one sentence printf, when we think of learning arrays, there is an operation called array traversal that is similar. If we take a closer look at these structures, we can easily find a judgment condition, and the next pointer of the last structure is bound to point to NULL. So using this, we can define a student_t structure pointer seek, which first points to the first structure variable, and when the output is complete, points to the second structure variable, that is, the first next pointer is assigned to the seek pointer, and so on. When the last next is assigned to the seek pointer, the value of the seek pointer is bound to be NULL, so you can exit the entire loop. The specific code is shown in figure 7.
Fig. 7 seek pointer variable these four structural variables
This is the end of the content of "how to link two lonely structural variables in C language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.