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 a structure in C language

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to define a structure in C language. The editor thinks it is very practical, so I hope you can get something after reading this article. Let's take a look at it with the editor.

First of all, think about a problem, suppose that one day you go to a planning company and receive a planning demand, such as writing a marketing copy for a newly listed mobile phone and printing it on your computer. So when writing this copy, you will definitely need to focus on some information about this phone, such as: phone brand: pear, model: 13mini, body length: 150.9mm, body width: 75.7mm, body thickness: 8.3mm, screen size 6.1in, pricing 5899.00, and so on. To implement this program is very simple, just to define these variables, the definition is completed and then printed on the computer screen, as shown in figure 1. Figure 1 defines variables to describe the phone parameters this program is very simple to achieve, but also very smooth to print out the phone's main introduction information. So the second question arises, assuming that the company has now learned how to market, and several phones will be released at a press conference. These phones are named 13miniLigue 13MaxPower13 MaxPlus respectively, and the parameter information of all dimensions of these phones is printed out on a poster, as shown in figure 1, so what should we do at this time? The easiest way is to define a variable for each mobile phone brand, model, body length, body width, body thickness, screen size, pricing, and so on, such as model information, Pear13 can be defined as Pear13_model,Pear13 mini can be defined as Pear13Mini_model, and so on. Just imagine, there are seven messages for each phone, so the four configured phones have to define a total of 28 different variables. So many variables are unfriendly to programming because so many independent variables can make the program look so messy that even the programmer himself can't figure out what these variables mean. If we take a closer look at these variables, in fact, the variables of each phone are fixed, that is, the seven dimensions of mobile phone brand, model, body length, body width, body thickness, screen size, and pricing. So can we combine these seven information into a set, and each time we add a new mobile phone variable, we only need to add one set to describe all the seven dimensions at the same time? This idea is obviously reasonable. In C language, this kind of composite variable composed of multiple basic variables is called a structure. Of course, this definition needs to be added that one structure can also contain another structure. Structure is defined in C language using the keyword "struct", which can be defined in two ways. The first way is to define the structure variable when defining the structure type, which is in the form of struct structure tag {data type member variable name; data type member variable name. } structure variable name; the second way is to define the structure type first, and then define the structure variable, which is in the form of structstruct structure label {data type member variable name; data type member variable name. }; / / define structure type struct structure label structure variable 1, structure variable 2. When using the first method, the structure tag can be omitted. When many readers come into contact with structures for the first time, they may often be confused about the structure labels, structure variable names, and the relationship between structure members. We can give a very vivid example to explain. Human thinking has a very powerful function, which is generality or identity, which shows that everyone is very good at finding rules in order to summarize the characteristics of some objective things. For example, when you describe Xiao Li's appearance, you will say, "she has big eyes, dark eyes, long linen hair, moonlight white skin, high nose, cherry mouth, etc." in fact, when you describe it in this way, you have abstracted a person's eyes, eyes, hair, skin, nose, mouth and other information, and everyone has this information. The information here is equivalent to its member variables in the structure. All this information is contained in the tag "person", so the tag "person" is similar to the tag of the structure. Finally, you describe the object Xiao Li, here is similar to the specific structural variable name, of course, you can use the same information to describe another person, such as Xiaoyue. Back to the point, how to use structural variables to describe the above mobile phone information? First, we define a structure tag, and then use this tag to define the description information of four specific mobile phones. This structure tag should contain seven member variables: mobile phone brand, model, body length, body width, body thickness, screen size and pricing. The structure tag can be written as: struct Mobilephone {char * brand; char * model; float length; float width; float thickness; float screen_size; float price;}. Then, use this structure tag to define the description information of four specific mobile phones, that is, to define four structure variables. Once the structural variables Mobilephone Pear13MiniInfo, Pear13Info, and Pear13MaxInfo,Pear13MaxPlusInfo; are defined, we can use them as if they were normal variables. Use the decimal point when using it. To get the member variables in each structure variable, if you want to assign a value to the brand variable in Pear13MiniInfo, you only need to use Pear13MiniInfo. Brand= "Pear" is fine. Therefore, the above program can be written as shown in figure 2.

The above is how the C language defines a structure, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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