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

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to define structure in C language". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to define structures in C language.

What is a structure?

In C language, struct refers to a kind of data structure, which is a kind of aggregated data type (aggregate data type) in C language. Structures can be declared as variables, pointers, or arrays to implement more complex data structures. A structure is also a collection of elements called members of the structure (member), which can be of different types and are generally accessed by name.

Comparison between structure and array

(1) it is made up of multiple elements.

(2) the storage space of each element in memory is continuous.

(3) the data type of each element in the array is the same, but the data type of each element in the structure can be different.

Definition of structure:

The definition template for C language structure types is approximately as follows:

Struct type name {member table column} variable

There can be several basic data types or structure types in the member table column.

Struct type name {} variable; semicolon cannot be left out

Here are several ways to define the type of structure

1. Define the structure type first, and then define the structure variable.

Struct student {char no [20]; / / Student ID char name [20]; / / name char sex [5]; / / gender int age; / / Age}; struct student stu1,stu2;// where stu1,stu2 is the student structural variable

two。 Define the structure variable as well as the structure type.

Struct student {char no [20]; / / Student ID char name [20]; / / name char sex [5]; / / gender int age; / / Age} stu1,stu2

At this point, you can also continue to define student structure variables such as:

Struct student stu3

3. Define structural body variables directly.

Struct {char no [20]; / / Student ID char name [20]; / / name char sex [5]; / / gender int age; / / Age} stu1,stu2

The third method of definition is generally not used, because you cannot continue to define variables of that type after you directly define the structural body variable stu1,stu2.

Note:

Struct cannot be omitted when defining structure variables after using struct to define structure types in C language, but struct is allowed to be omitted in C++.

In c:

Struct student {...}; struct student stu1; / / struct cannot be omitted

In C++:

Struct student {...}; student stu1; / / struct can be omitted

After defining the structure type in C, we should use struct every time we define a variable. If we find it troublesome, we can do this:

Typedef struct student {...} STUDENT;STUDENT stu1

Use typedef to give struct student an "alias" STUDENT

In some cases, # define can also be used to implement more simplified structure and variable definitions, but some readability may be sacrificed.

# define STUDENT struct student;STUDENT {.}; STUDENT stu1; so far, I believe you have a deeper understanding of "how to define structures in C language". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report