1. Structure type definition
Definition method 1:
Typedef struct LNode {
int data; // 数据域
struct LNode *next; // 指针域
} *LinkList;
Definition method 2:
struct LNode {
int data; // 数据域
struct LNode *next; // 指针域
};
Typedef struct LNode *LinkList;
The above two definition methods are equivalent, that is, to define*LinkListdefined asstruct LNodetype, that isLinkListis defined as a type name. In this way, you can useLinkListto define and declare new variables, such as:
LinkList L;
that is, to defineLdefined asstruct LNodepointer variable of type.
2. Structure type variable definition
Definition method 1:
struct LNode {
int data; // 数据域
struct LNode *next; // 指针域
}LnodeA;
Definition method 2:
struct LNode {
int data; // 数据域
struct LNode *next; // 指针域
};
struct LNode LnodeA;
The above two definition methods are also equivalent, in this way, defineLnodeAdefined as astruct LNodevariable of type, that isLnodeAas astruct LNodevariable name of type.
Definition methods for structures and internal tables
1. Structure, transparent table area, DATA ELEMENT, DOMAIN
A transparent table is a logical description of a physical table. The transparent table contains many fields, and some fields are defined as PRIMARY KEY. The fields also contain DATA ELEMENT, which is used to describe language attributes and technical attributes. DATA ELEMENT also contains DOMAIN, which defines data type and field length.
A structure is generally used to define a structure variable, with storage of temporary data, and no PRIMARY KEY. A structure contains COMPONENT rather than FIELD.
2. The attributes in an internal table are divided into three types: LINE TYPE, KEY, TABLE KIND.
LINE TYPE: A single row in an INTERNAL TABLE is called LINE TYPE, and the structure of each row is the same.
KEY: Equivalent to the primary key in a database. It is useful when sorting, UNIQUE or NON-UNIQUE.
TABLE KIND: It is divided into STANDARD, SORTED, HASHED.
Definition of the three types of internal tables
Standard table:
DATA itab1 TYPE STANDARD TABLE OF scarr WITH NON-UNIQUE KEY carrid.
Sorted table:
DATA itab2 TYPE SORTED TABLE OF scarr WITH NON-UNIQUE KEY carrid.
Hashed table:
DATA itab3 TYPE HASHED TABLE OF scarr WITH UNIQUE KEY carrid.
General definition method (standard table)
Define a standard table based on a database table
DATA itab4 TYPE TABLE OF scarr.
Define a standard table based on a self-defined structure variable (most common)
DATA: BEGIN OF wa,
carrid TYPE scarr-carrid,
carrnamen TYPE scarr-carrname,
END OF wa.
DATA itab5 LIKE TABLE OF wa.
Define a standard table based on a table type in the data dictionary
DATA itab6 TYPE ztabtype00_1.
Define an internal table based on an internal table
DATA itab7 LIKE itab6.
Define a structure based on an internal table
DATA wa1 LIKE LINE OF itab7.
3. Ways to define a structure
Define a program's structure variable (or type) based on a table or structure in the data dictionary
types str1 type spfli. data str2 type sflight.
Self-define a structure variable (or type) in a program - most common
data: begin of wa, carrid type spfli-carrid, com(10) type c, end of wa.
Define a structure based on an internal table
data wa like line of itab.
Note:What is defined based on a database table must be a structure
Assignment of fields with the same name (important)
Move-corresponding A to B.
READ is to read one record from an internal table
read table itab like table of wa.
To read multiple records from an internal table, use LOOP
loop at itab into wa.
Explanation in C/C++ language
Structure definition
A structure (struct) is a data collection composed of a series of data of the same type or different types, also called a structure.
Function of structures
A structure is like other basic data types, such as int type, char type; only a structure can be made into the data type you want, to facilitate future use.
In actual projects, structures exist in large numbers. R&D personnel often use structures to encapsulate some properties to form new types.
The role of structures in functions is not convenience; its main role is encapsulation. The benefit of encapsulation is that it can be reused. It allows users not to care about what this is, just use it according to the definition.
Size of structures and memory alignment
The size of a structure is not simply the sum of its elements, because the mainstream computers we use now have CPUs with 32-bit word length. For this type of CPU, reading a number of 4 bytes is more efficient and more convenient than reading one byte. Therefore, if the first address of each member in a structure is a multiple of 4, fetching data elements will be relatively more efficient. This is the origin of memory alignment.
The compiler on each specific platform has its own default "alignment factor" (also called alignment modulus). Programmers can use the precompilation command#pragma pack(n),n=1,2,4,8,16 to change this factor, where n is the "alignment factor" you want to specify.
Rules:
- 1. Data member alignment rule: For the data members of a structure (struct) (or union), the first data member is placed at offset 0, and each subsequent data member is aligned according to#pragma packthe smaller of the specified value and the length of the data member itself.
- 2. Overall alignment rule of a structure (or union): After each data member has completed its own alignment, the structure (or union) itself must also be aligned. The alignment will be based on< span class="marked">#pragma packthe smaller of the specified value and the length of the largest data member in the structure (or union).
- 3. Combining 1 and 2 to infer: When the n value of #pragma pack is equal to or exceeds the lengths of all data members, the size of this n value will not have any effect.
Structures in C++
In C language, you can define structure types to package multiple related variables as a whole. Variables in a structure can be of the same, partially the same, or completely different data types. In C language, structures cannot contain functions. In object-oriented programming, objects have state (attributes) and behavior; state is stored in member variables, and behavior is implemented through member methods (functions). Structures in C can only describe the state of an object, not the behavior of an object. In C++, considering the continuity of the transition from C to C++, structures are extended. Structures in C++ can contain functions, so structures in C++ also have the functionality of classes. Unlike class, functions contained in a structure are public by default, not private.
C++ console output example:
Example
Differences between structures and classes in C++
In C++, classes and structures differ in only two ways; apart from this, there are no differences.
- (1) The default member access permission in class is private, while in struct it is public.
- (2) Inheritance from class is private by default, while inheritance from struct is public by default.
What is the difference between these two definitions?
typedef struct student
{
int num;
struct student *next;
}student;
struct student
{
int num;
struct student *next;
};
The second "struct student" defines a student structure, do you understand this?
The first uses typedef to redefine the structure type name "struct student" as "student". That is, "struct student" and "student" represent the same thing and are both identifiers of a type. For example, "typedef int zhengshu;" renames the integer int to zhengshu. The following definitions: "int i;" and "zhengshu i;" are equivalent.
A structure is a combination of various variables that are composed of basic data types and named by an identifier. Different data types can be used in a structure.
Structure declaration and structure variable definition
In Turbo C, a structure is also a data type, and structure variables can be used. Therefore, like variables of other types, structure variables must be defined before use.
The general format for defining a structure variable is:
struct 结构名
{
类型 变量名;
类型 变量名;
...
} 结构变量;
The structure name is the identifier of the structure, not the variable name.
The types are the five data types described in Section 2 (integer, floating-point, character, pointer, and void).
Each type variable that makes up a structure is called a structure member. It is like an array element, but array elements are accessed by subscripts, while structures access members by variable names.
The following is an example to illustrate how to define a structure variable.
struct string
{
char name[8];
int age;
char sex[4];
char depart[20];
float wage1,wage2,wage3;
}person;
This example defines a structure variable "person" with a structure name "string". If the variable name "person" is omitted, it becomes a declaration of the structure. An already declared structure name can also be used to define structure variables. When defined this way, the above example becomes:
struct string
{
char name[8];
int age;
char sex[4];
char depart[20];
float wage1,wage2,wage3;
};
struct string person;
If you need to define multiple structure variables with the same form, this method is more convenient. It first declares the structure, then uses the structure name to define the variables.
For example:
struct string Tianyr, Liuqi, ...;
If the structure name is omitted, it is called an anonymous structure. This situation often occurs inside functions. When using this kind of structure, the previous example becomes:
struct
{
char name[8];
int age;
char sex[4];
char depart[20];
float wage1,wage2,wage3;
} Tianyr, Liuqi;
Use of structure variables
A structure is a new data type, so structure variables can be assigned and operated on like variables of other types. The difference is that structure variables use members as basic variables.
The representation of structure members is:
structure variable.member name
If you regard structure variable.member name as a whole, the data type of this whole is the same as the data type of that member in the structure, so it can be used like the variables described earlier.
The following example defines a structure variable, where each member receives data from the keyboard, then sums the floating-point numbers in the structure and displays the result. Please note the access to different structure members in this example.
Example
Structure arrays and structure pointers
A structure is a new data type, and it can also have structure arrays and structure pointers.
1. Structure arrays
A structure array is a collection of variables with the same structure type. For example, to define the names, genders, ages, and addresses of 40 students in a class, you can define it as a structure array. As shown below:
struct
{
char name[8];
char sex[4];
int age;
char addr[40];
}student[40];
It can also be defined as:
struct string
{
char name[8];
char sex[4];
int age;
char addr[40];
};
struct string student[40];
It should be pointed out that the access to structure array members takes the array element as a structure variable, and its form is:
structure array element.member name
For example:
student[0].name student[30].age
In fact, a structure array is equivalent to a two-dimensional structure. The first dimension is the structure array elements, each element is a structure variable, and the second dimension is the structure members.
Note: Members of a structure array can also be array variables.
For example:
struct a
{
int m[3][5];
float f;
char s[20];
}y[4];
To access the structure variable in structure ay[2]this variable, it can be written asy[2].m[1][4]
2. Structure pointers
A structure pointer is a pointer to a structure. It is defined by a * operator placed before the structure variable name. For example, use the previously described structure to define a structure pointer as follows:
struct string
{
char name[8];
char sex[4];
int age;
char addr[40];
}*student;
You can also omit the structure pointer name and only make a structure declaration, then use the following statement to define the structure pointer.
struct string *student;
Using a structure pointer to access structure members is different in expression from using a structure variable to access structure members. Access to structure members via a structure pointer is expressed as:
structure pointer name->structure member
where->are two symbols-and>combined, like an arrow pointing to a structure member. For example, to assign values to name and age in the structure defined above, you can use the following statements:
strcpy(student->name, Lu G.C); student->age=18;
In fact,student->nameis(*student).namethe abbreviation of.
It should be pointed out that a structure pointer is a pointer to a structure, that is, the first address of the first member in the structure. Therefore, before use, the structure pointer should be initialized, that is, allocate a byte space of the entire structure length. This can be done with the following function, still using the previous example to illustrate as follows:
student=(struct string*)malloc(size of (struct string));
size of (struct string)Automatically obtain the byte length of the string structure. The malloc() function defines a memory area of the size of the structure length, and then returns its first address as the structure pointer.
Note:
- 1. A structure is a data type, so the defined structure variables or structure pointer variables also have local variables and global variables, depending on where they are defined.
- 2. The structure variable name is not the address pointing to the structure, which is different from the meaning of an array name. Therefore, if you need to find the first address of the first member in the structure, it should be&[structure variable name]。
- 3. Complex form of structure: nested structures
A nested structure means that a structure member can include another structure. Turbo C allows this kind of nesting.
For example: The following is a structure with nesting
struct string
{
char name[8];
int age;
struct addr address;
} student;
Here: addr is the structure name of another structure, and it must be declared first, that is,
struct addr
{
char city[20];
unsigned lon zipcode;
char tel[14];
}
If you want to assign a value to zipcode in the address structure, which is a member of the student structure, you can write:
student.address.zipcode=200001;
Each structure member name is listed one by one from the outermost layer to the innermost layer, that is, the expression of nested structure members is:
structure variable name.nested structure variable name.structure member name
There can be many nested structures, and the structure member name is the name of a member in the innermost structure that is not a structure.
Original link: https://www.cnblogs.com/yuxinJ/p/4688496.html