Summary of methods defined by C language constructs

  • 2020-06-01 10:30:19
  • OfStack

What is a structure?

In C, a structure (struct) is a data structure, which is a class of aggregated data types (aggregate data type) in C. Structs can be declared as variables, Pointers, arrays, etc., to implement more complex data structures. A struct is also a collection of elements called members of a structure (member), which can be accessed by name for different types.

Comparison of structs and arrays

(1) are composed of multiple elements

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

(3) the data types of each element in the array are the same, while the data types of each element in the structure can be different

Definition of structure:

The definition template for the C language struct type is roughly:


struct  Type name {
   Members of the columns of the table 
}  variable ;

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

struct type name {} variable; After the semicolon can not be missed

Here are some ways to define a struct type

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


struct student{
  char no[20];    // Student id 
  char name[20];  // The name 
   char sex[5];  // gender 
  int age;     // age 
};       
struct student stu1,stu2;
// At this time stu1,stu2 for student Structural variable 

2. Define struct variables while defining struct types.


struct student{
  char no[20];    // Student id 
  char name[20];   // The name 
   char sex[5];   // gender 
  int age;      // age 
} stu1,stu2;   

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


struct student stu3;

3. Directly define structural variables.


struct{
  char no[20];    // Student id 
  char name[20];   // The name 
   char sex[5];   // gender 
  int age;     // age 
} stu1,stu2; 

The third definition method is generally not used in 1, because the structure variable stu1 is defined directly, and stu2 cannot continue to define variables of this type.

Note:

In the C language, struct cannot be omitted when using struct to define struct types and then defining struct variables. struct is allowed to be omitted in C++.

In c:


struct student{
...
};
struct student stu1;  //struct Do not omit 

In c + + :


struct student{
...
};
student stu1;  //struct Can be omitted 

After defining the struct type in C, struct is used every time you define a variable. If it's too much trouble, we can do this:


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

Use typedef to give struct student an "alias "STUDENT

You can also use #define in some cases to achieve a simpler structure definition and variable definition, but at the cost of some readability.


#define STUDENT struct student;
STUDENT{
...
};
STUDENT stu1;

The usage of typedef and #define is different and can even be used flexibly in combination. You should always pay attention to the differences between the two when using typedef and #define.

conclusion


Related articles: