Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define structure in C language

Shulou Source: shulou.com Published: 2022-06-01 01:47:45 09月23日 Update

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!

Tags: Structure type variable language element data member name student number age gender array method same content at the same time data structure learning different complex Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple Microsoft NVidia Huawei