C Language, structural body (struct) detailed usage

Source: Internet
Author: User
Tags bitwise operators

Structure (struct)
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.
1. Structure description and Structure variable definition
In Turbo C, a struct is also a data type that can use structural variables, so, like other
A variable of type, you define it first when you use a struct variable.
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 type).
Each type variable that constitutes a struct is called a struct member, like an element of an array, but in an array
The element is accessed by the following tag, and the struct accesses the member by the variable name.
Here's an example of how to define a struct variable.
struct string
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
} person;
This example defines a struct variable person with a struct named string, if the variable name is omitted
person, it becomes a description of the structure. Structure variables can also be defined with the described structure name. This defines
When the previous example becomes:
struct string
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
};
struct string person;
It is convenient to use this method if you need to define multiple structural variables with the same form.
Structure description, and then use the struct 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 appears inside the function, using the
Structure, the preceding example becomes:
struct
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
} Tianyr, Liuqi;
2. Use of structural variables
A struct is a new data type, so a struct variable can also be assigned as a variable of other types,
Operations, the difference is that the struct variable is a member as the basic variable.
Struct members are represented by:
struct variable. Member name
If you consider a "struct variable. Member name" as a whole, then the data type of the whole and the structure
The data type of the member is the same, 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 and then
Sums the floating-point numbers in the structure and displays the result of the operation, while storing the data in a text-named
Wage.dat in the disk file. Note the access of different struct members in this example.
Example 3:
#i nclude <stdio.h>
Main ()
{
struct{/* Define a struct variable */
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4,
Wage5;
}a;
FILE *FP;
float wage;
Char c= ' Y ';
Fp=fopen ("Wage.dat", "w");
/* Create a file to write only */
while (c== ' Y ' | | c== ' y ')
/* Determine if the loop continues */
{
printf ("\nname:");
scanf ("%s", a.name); /* Enter your name */
printf ("Age:");
scanf ("%d", &a.wage); /* Enter Age */
printf ("Sex:");
scanf ("%d", a.sex);
printf ("Dept:");
scanf ("%s", A.depart);
printf ("Wage1:");
scanf ("%f", &a.wage1); /* Enter Salary */
printf ("Wage2:");
scanf ("%f", &a.wage2);
printf ("Wage3:");
scanf ("%f", &a.wage3);
printf ("Wage4:");
scanf ("%f", &a.wage4);
printf ("Wage5:");
scanf ("%f", &a.wage5);

Wage=a.wage1+a.wage2+a.wage3+a.wage4+a.wage5;
printf ("The sum of wage is
%6.2f\n ", wage);/* Show Results */
fprintf (FP,
"%10s%4d%4s%30s%10.2f\n",/* results written to file */
A.name, A.age, A.sex,
A.depart, wage);
while (1)
{
printf ("continue?<y/n>");
C=getche ();

if (c== ' Y ' | | c== ' Y ' | | c== ' N ' | | c== ' n ')
Break
}
}
Fclose (FP);
}
3. Structure arrays and pointers to structures
A struct is a new type of data, as well as an array of structures and pointers to structures.
An array of structures
An array of structures is a collection of variables with the same structure type. If you want to define a class of 40 classmates
's name, gender, age, and address can be defined as an array of structures. As shown below:
struct{
Char Name[8];
Char sex[2];
int age;
Char addr[40];
}STUDENT[40];
can also be defined as:
struct string{
Char Name[8];
Char sex[2];
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 struct 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]
Second, the structure of the pointer
A structure pointer is a pointer to a structure. It is determined by a "*" operator that is added to the structure variable name.
For example, define a structure pointer with the previously described structure as follows:
struct string{
Char Name[8];
Char sex[2];
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, and structure variables access to struct members in the expression mode
Different on the other. Structure pointers access to struct members is represented by:
struct pointer named struct member
where "--" is a combination of two symbols "-" and ">", as if an arrow points to a struct member. For example to
To assign the name and age to 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 is necessary to point out that the structure pointer is a pointer to the structure, that is, the first member of the structure
The structure pointer should be initialized before use, that is, the byte space allocated for the entire length of the structure,
This can be done using the following function, as explained in the example above:
student= (struct string*) malloc (Size of
(struct string));
Size of (struct string) automatically takes the byte length of the string structure,
malloc () function
Defines an area of memory that is of a structure length and returns its fraudulent address as a struct pointer.
Attention:
1. Structure as a data type, so the defined structure variable or structure pointer variable also has the office
Variables and whole variables, depending on the location of the definition.
2. The structure variable name is not an address to the structure, which is different from the meaning of the array name, so if you want to
Requires that the first member of the structure should be the &[structure variable name].
4. Complex form of structure
One, nested structure
A nested structure refers to a struct member that can include other structures, and Turbo C allows this
Nested.
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 inner one, that is, the expression of the nested struct member
The way is:
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.

Second, bit structure
Bit structure is a special structure that, when a bit is required to access a byte or a number of bits of a word, the bit structure
More convenient than bitwise operators.
The general form of a bit structure definition is:
struct bit struct name {
Data type variable name: integer constant;
Data type variable name: integer constant;
} bit structure variable;
Where: The data type must be int (unsigned or signed). The integer constant must be a non-negative integer
Number, the range is 0~15, which represents the number of bits, that is, how many bits.
The variable name is the selection and can be not named, so the rule is to arrange the need.
For example, a bit structure is defined below.
struct{
unsigned incon:8;
/*incon occupies a low byte 0~7 total of 8 bits */
Unsigned txcolor:
4;/*txcolor High-byte 0~3 bits total 4 bits */
unsigned bgcolor:
3;/*bgcolor High-byte 4~6 bits total 3 bits */
unsigned blink:1; /*blink occupies a high byte of the 7th bit */
}ch;
The access of a bit struct member is the same as that of a struct member.
For example, access to the bgcolor member in the previous example bit structure can be written as:
Ch.bgcolor

Attention:
1. Members in a bit structure can be defined as unsigned, or as signed, but when the members are long
is considered to be the unsigned type at 1 o'clock. Because a single bit cannot have a symbol.
2. Members in a bit structure cannot use arrays and pointers, but bit structure variables can be arrays and pointers,
If it is a pointer, its members are accessed in the same way as the struct pointer.
3. The total length of the bit structure (number of digits), is the sum of the bits defined by each bit member, can be more than two words
Section.
4. Bit structure members can be used with other struct members.
For example:
struct info{
Char Name[8];
int age;
struct addr address;
float pay;
unsigned state:1;
unsigned pay:1;
}workers; '
The structure of the example above defines the information about a worker. There are two bit structure members, each of which has a node
Only one byte but holds two information, the first in the byte indicates that the
Status of the person, and the second indicates whether the pay has been paid. This shows that the use of bit structure can save storage space.

http://user.qzone.qq.com/2080085912/blog/1424355551
http://user.qzone.qq.com/2080085912/blog/1424356861
http://user.qzone.qq.com/2080085912/blog/1424356978
http://user.qzone.qq.com/2080085912/blog/1424357052
http://user.qzone.qq.com/2080085912/blog/1424357146
http://user.qzone.qq.com/2080085912/blog/1424357241

C Language, structural body (struct) detailed usage

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.