Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define pointers and structures in C language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to define the pointer and structure of C language". The editor shows you the operation process through an actual case, which is simple, fast and practical. I hope this article "how to define the pointer and structure of C language" can help you solve the problem.

Pointers and structures

When we talked about pointers earlier, we have already introduced the use of pointers to point to ordinary variables, point to functions, point to arrays, point to strings and other operations, because the structure was not introduced at that time, so there is no description of pointer to structure. However, as the most iconic feature of C language, pointers pointing to structures must also be supported, and pointers to structures are the structure pointers to be discussed in this section.

Before we talk about structure pointers, let's define a structure. The step to define this structure is to first declare the storage type of a structure with the keyword "typedef".

Typedef struct

{

Char name [20]

Int age

Int id_num

} student_t

Next, use this structure type to define a structure variable, "student_t Xiaoming;".

When the structure variable describing Xiaoming information is defined, the compiler actually opens up a memory area above the memory to store the structure variable, as shown in figure 1.

Fig. 1 in vivo storage and discharge of the structure

After the structure memory is defined, we can use the form of "Xiaoming. Structure member name" to access any member of the structure memory. So what exactly does the name of this structural variable represent? We can use the printf function to print out the name of the structure variable and the address of each internal member variable, as shown in figure 2. Obviously, the address of the variable name of the structure is the same as the first address of the first member variable in the structure, so we can infer that the address of the name of the variable represents the header address of the structure.

Fig. 2 address of structure variable name and address of member variable

Based on what has been discussed above, we can try to define a pointer to this structure. But what type should this pointer define? As we said earlier when we talked about the basis of pointers, the data type of a pointer to a variable should be consistent with what it points to. So are we going to use pointers of type struct, such as "struct * pt", for pointers to this structure?

Obviously, this is wrong, because we discussed earlier why the data type of a pointer to a variable should be consistent with what it points to. The fundamental reason is to keep the growth of the pointer consistent with its memory. Therefore, if you define a pointer like "struct * pt", it must be meaningless and grammatically incorrect.

As we said earlier when we talked about structures, when the struct keyword is used to declare a structure, it actually declares the storage type of the structure, so for this structure, we have declared it as a new data type with typedef, so the pointer to be defined should be "student_t * stu_pt;".

Now that the pointer to the structure is defined, the next content should be how to point to the structure. According to our previous experience, the pointing operation is actually assigning the address of the pointing object to a pointer variable, so the pointing operation is very simple, "stu_pt = & Xiaoming". As shown in figure 3.

Figure 3 defines the pointer to the structure

We can see from figure 3 that the content of the pointer is the same as the first address of the structure it points to, so we can preliminarily judge that the structure pointing operation is correct.

Now that we know how to define a structure pointer and how to use it to point to a structure, here comes the problem. the original intention of defining a structure is that several related variables are arranged in an orderly manner, and every member of the structure variable can be referenced, read and written, and now We have defined a structure pointer and pointed to a structure variable, so can we use this structure pointer to refer to the member variables in the structure it points to? If so, how should I quote it?

Before we answer this question, let's recall that when a structure variable references a pointer, it uses the form of "structure variable name. Member variable name". Then for a structure pointer, this way of referencing the member name of a structure variable is not allowed. For a structure pointer to a structure variable, we should use the pointing symbol "- >" to refer to the member variable of the structure variable that the structure pointer points to. This must be borne in mind.

For example, we can use the "student_t" type to define a structure variable Xiaoming, and then use a structure pointer to point to this variable. We first assign a value to the member variable name in the structure variable Xiaoming, and then use the structure pointer to read it out. Then use the structure pointer to assign values to the age variable, and then use the structure variable to read it out, as shown in figure 4.

Fig. 4 structure pointer access structure variable

This is the end of the introduction on "how to define pointers and structures in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report