Definition of structure variables and structure types in language

Source: Internet
Author: User
Tags table definition

1. structure type definition

definition Mode 1 :

Typedef struct Lnode {

int data; data fields

struct Lnode *next; pointer Field

} *linklist;

Definition Mode 2 :

struct Lnode {

int data; data fields

struct Lnode *next; pointer Field

};

Typedef struct Lnode *linklist;

The above two definitions are equivalent and will be *linklist defined as struct Lnode type, i.e. linklist is defined as a type name. This allows you to define new variables using linklist, such as:

linklist L ;

soon L defined as struct Lnode pointer variable of type

2. structure type variable definition

definition Mode 1 :

struct Lnode {

int data; data fields

struct Lnode *next; pointer Field

}lnodea;

Definition Mode 2 :

struct Lnode {

int data; data fields

struct Lnode *next; pointer Field

};

struct Lnode Lnodea ;

The above two definition methods are also equivalent, so that the Lnodea defined as a truct lnode type of variable, i.e. Lnodea for a truct lnode the variable name of the type.

How to define the body of a structure

1. Structure, transparent table area, DATA Element,domain

Transparent tables are logical descriptions of a physical table, there are many fields in the transparent table, and some are defined as primary key, and the field contains the data ELEMENT, which is used to describe language attributes and technical attributes. The data element also contains domain, which is defined as the type and length of the field

Structs are typically used to define a structure variable, have temporary data stored, no primary KEY, and the struct contains component instead of field

The attributes in the 2.Internal TABLE are divided into three line types, key,table KIND.

Line type in internal table is called line type, and the structure of each row is the same.

Key: Equivalent to the primary key in the database. Useful when sorting, UNIQUE or Non-unique.

TABLE KIND: Divided into standard,sorted,hashed.

Definition of three types of internal tables

* Standard table:

DATA itab1 TYPE Standard TABLE of Scarr with Non-unique KEY Carrid.

* Sort table:

DATA itab2 TYPE SORTED TABLE of Scarr with Non-unique KEY Carrid.

Hash table

DATA itab3 TYPE HASHED TABLE of Scarr with UNIQUE KEY Carrid.

General definition Method (standard table)

* Standard tables defined according to database tables

DATA itab4 TYPE TABLE of Scarr.

* Standard tables are defined according to the self-built structural variables (most commonly used)

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 the table type in the data dictionary

DATA itab6 TYPE ztabtype00_1.

* The inner table is defined according to the inner table

DATA itab7 like Itab6.

Define the structure according to the inner table

DATA wa1 like Line of Itab7.

3. How the structure is defined

* Define the structure variables (or types) of the program according to the table or structure in the data dictionary

Types STR1 type Spfli.

Data str2 type Sflight.

* Structure variables (or types) in custom programs-the most common

Data:begin of WA,

Carrid type Spfli-carrid,

COM (ten) Type C,

End of WA.

* Define the structure according to the inner table

Data wa like line of Itab.

* Note: it must be structured according to the database table definition

* Assignment of the same name field (important)

Move-corresponding A to B.

*read is a piece of data that reads inside a table

Read table itab like Table of WA.

* Read a list of multiple data applications loop

Loop at Itab to WA.

C + + explanations in the language

Definition of structural body

A struct is a collection of data, also called a structure, that consists of a series of data of the same type or different types.

Structure function

Structs are the same as other types of underlying data types, such as int types, and the char type is just a struct that can be made into the type of data you want. To facilitate future use.

In the actual project, there is a large number of structures. Developers often use structs to encapsulate properties to form new types.

The function of the structure is not simple, its main function is encapsulation. The advantage of encapsulation is that it can be reused. Let the user not care about what this is, as long as it is used according to the definition.

the size of the structure and Memory Alignment

The size of the struct is not simply the sum of the elements of the structure, because we are now using 32Bit - word CPUfor the main computer, for this type of CPU It is more efficient and convenient to take 4 bytes than to take a single byte. So the first address of each member in the struct is 4 integer multiples, and the data element is relatively more efficient, which is the origin of memory alignment.

Compilers on each particular platform have their own default "alignment factor" (also known as the number of Zimo). Programmers can change this factor by precompiling the command #pragma pack (n), where n is the "alignment factor" you want to specify.

Rules:

1. Data member Alignment rules: data members of a struct (struct) (or union), where the first data member is placed at offset 0, and subsequent alignment of each data member according to the value specified by the #pragma pack and the length of the data member itself, than the smaller one.

2, structure (or union) of the overall alignment rules: After the data members have completed their respective alignment, the structure (or union) itself will be aligned, alignment will follow the value specified by the #pragma pack and the structure (or union) of the maximum data member length, the smaller one.

3, combined with 1, 2 inference: When the n value of the #pragma pack equals or exceeds the length of all data members, the size of the N value will have no effect.

C + + the structure in

In the C language, you can define a struct type and wrap multiple related variables into one overall use. Variables in structs can be of the same, partially identical, or completely different data types. in the C in language, structs cannot contain function . in object-oriented programming , objects have state (attributes) and behavior, state is persisted in member variables, and behavior is implemented by member methods (functions). A struct in C can only describe the state of an object and cannot describe the behavior of an object. in the C + + , taking into account the C language to C + + The continuity of language transition, the structure is extended, C + + structure can contain functions, so that the C + + the structure of the body also has class the function, with class The difference is that the struct contains a function that defaults to Public , but not Private .  

Example of C + + console output:

#include <cstdlib>

#include <iostream>//define Structure body

struct POINT

{

Consists of two variable members

int x;

int y;

};

using namespace Std;

int main (int argc, char *argv[])

{

struct Point pt;

Pt.x=1;

pt.y=2;

cout<<pt.x<<endl<<pt.y<<endl;

return exit_success;

}

C + + The difference between a struct and a class in a

There is no difference between a class and a struct in C + + only two points apart.

(1) The default member access rights in class are private, while structs are public.

(2) Inheriting from class is a private inheritance by default, while inheriting from struct defaults to public inheritance.



What is the difference between the two definitions? Thank you.

typedef struct STUDENT

{

int num;

struct student *next;

}student;

struct student

{

int num;

struct student *next;

};

The second struct student is the definition of a student struct, which is clear.

The first is to redefine the struct student as a student, i.e. struct student and student represent the same thing, which is a type identifier, such as typedef int Zhengshu; You rename int to Zhengshu, the following definition: int i; and Zhengshu I; The two sentences are equivalent.

A structure is a combination of various variables that consist of a basic data type and are named with a single identifier. Different data types can be used in the structure.

I. Structure description and Structure variable definition
In Turbo C, a struct is also a data type that can be used with structural variables, so, like other types of variables, it is defined first when the structure variable is used.
The general format for defining structure variables is:
struct structure name
{
Type variable name;
Type variable name;
...
} structure variables;
The struct name is a struct identifier and is not a variable name.
The type is the five data types described in section II (integer, float, character, pointer, and no-value).
Each type variable that constitutes a struct is called a struct member, like an element of an array, but the element in the array is accessed by the following tag, and the struct accesses the member by its name.
Here's an example of how to define a struct variable.

struct string
{
Char Name[8];
int age;
Char sex[4];
Char depart[20];
float Wage1,wage2,wage3;
}person;

This example defines a struct variable person with a struct named string, and if the variable name person is omitted, it becomes a description of the structure. Structure variables can also be defined with the described structure name. This is defined when the previous 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 more than one structure variable with the same form, it is convenient to use this method, it first makes the structure description, then uses the structure name to define the variable.
For example:
struct string tianyr, Liuqi, ...;
If you omit the struct name, it is called the nameless structure, which often occurs inside the function, and the previous example becomes:

struct
{
Char Name[8];
int age;
Char sex[4];
Char depart[20];
float Wage1,wage2,wage3;
} Tianyr, Liuqi;

Second, the use of structural variables
A struct is a new data type, so a struct variable can also be assigned a value, an operation, as a variable of other types, but a struct variable is a member as a basic variable.
Struct members are represented by:
struct variable. Member name
If you consider a struct variable, a member name as a whole, the data type of the whole is the same as the data type of the member in the struct, so it can be used as previously mentioned variables.
The following example defines a struct variable in which each member receives data from the keyboard, sums the floating-point numbers in the structure, and displays the result of the operation. Note the access of different struct members in this example.

#include
Main ()
{
struct
{
Char Name[8];
int age;
Char sex[4];
Char depart[20];
float Wage1,wage2,wage3;
}a;
float wage;
Char c= ' Y ';
while (c== ' Y ' | | c== ' y ')
{
printf (\nname:);
scanf (%s, a.name);
printf (age:);
scanf (%d, &a.wage);
printf (Sex:);
scanf (%s, a.sex);
printf (Dept:);
scanf (%s, a.depart);
printf (Wage1:);
scanf (%f, &a.wage1);
printf (Wage2:);
scanf (%f, &a.wage2);
printf (Wage3:);
scanf (%f, &a.wage3);
Wage=a.wage1+a.wage2+a.wage3;
printf (the sum of wage is%6.2f\n, wage);
printf (Continue?);
C=getche ();
}
}

III. structure arrays and structure pointers
A struct is a new type of data, as well as an array of structures and pointers to structures.
1. Array of structures
An array of structures is a collection of variables with the same structure type. If you want to define a class 40 students name, gender, age and address, can be defined as an array of structures. As shown below:
struct
{
Char Name[8];
Char sex[4];
int age;
Char addr[40];
}STUDENT[40];
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 noted that access to struct array members is based on the array element as a structural variable, in the form of:
The structure array element. Member name
For example:
Student[0].name
Student[30].age
In fact, the structure array is equivalent to a two-dimensional construct, the first dimension is the structure array element, each element is a structure variable, and the second dimension is a struct member.
Attention:
A member of a struct array can also be an array variable.
For example:
struct A
{
int m[3][5];
float F;
Char s[20];
}Y[4];
In order to access this variable of struct variable y[2] in struct A, it can be written as y[2].m[1][4]
2. Structure pointers
A structure pointer is a pointer to a structure. It is defined by a * operator that is prepended to the structure variable name, such as defining a structure pointer with the previously described structure as follows:
struct string
{
Char Name[8];
Char sex[4];
int age;
Char addr[40];
}*student;
You can also omit the struct pointer name for the structure description, and then define the structure pointer with the following statement.
struct string *student;
Access to struct members using struct pointers differs from structure members ' access to struct variables in the way they are expressed. Structure pointers access to struct members is represented by:
struct pointer named struct member
Which is a combination of two symbols-and >, as if an arrow points to a struct member. For example, to assign a value to name and age in the structure defined above, you can use the following statement:
strcpy (Student->name, Lu g.c);
student->age=18;
In fact, Student->name is the abbreviated form of (*student) name.
It should be pointed out that the structure pointer is a pointer to the structure, that is, the first member of the structure of the initial address, so before use should be initialized to the structure pointer, that is, the allocation of the entire length of the structure of the byte space, which can be completed by the following function, still described as follows:
student= (struct string*) malloc (size of struct string);
A size of (struct string) automatically takes the byte length of a string structure, and the malloc () function defines a memory area of a structure length, and then returns its fraudulent address as a struct pointer.

Attention:
1. Structure as a data type, so the definition of structural variables or structure pointer variables also have local variables and full variables, depending on the location of the definition.
2. The structure variable name does not point to the address of the structure, which is different from the meaning of the array name, so if you need to require that the first member of the struct be the &[struct variable name].
3. Complex form of structure: nested structure
Nested structures are those that can include other structures in a struct member, and Turbo C allows such nesting.
For example: The following is a nested structure
struct string
{
Char Name[8];
int age;
struct addr address;
} student;
Where: addr is the structure name of another structure, which must be done first, stating that
struct ADDR
{
Char city[20];
unsigned lon ZipCode;
Char tel[14];
}
If you want to assign a value to a zipcode in a member address structure in a student structure, you can write:
student.address.zipcode=200001;
Each struct member name is listed from the outermost layer up to the topmost level, which is how nested struct members are expressed:
Structure variable name. Nested struct variable name. struct Member name
Where: Nested structures can have many, struct member names are not struct member names in the most internal structure.

Definition of structure variables and structure types in language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.