1.6 C of the structure, union, enumeration, macro definitions, pretreatment

1. The structure
Why do we need a structure? What is the structure? Variable + voxel group
in the absence of the structure, at first, when the most simple, only requires the use of basic types (int char float dooubt) to define a single variable, several variables need to define a few. Later, the situation is complex, and sometimes required variables associated meaning (such as the need to store a fraction of students class) array at this time there have been. Array addresses the need for many of the same types of problems related to the significance of variables. But there are limits to the array, the array biggest shortcoming is that an array of the same variables can only store multiple data types. So just come to package several different types of variable, the array can do about it. This time he needs a structure.
What is the structure?
Structure is a collection, which is a kind comprising a number of elements, the elements of the data type may be the same, may not be the same. The method is therefore encapsulated data structure.
the include <stdio.h>
//. 1, structure type is defined as a function of the outside, not the inside
// 2, the structure is defined by a combination of a new type, rather than a variable, not consume memory

        struct  student
        {
            char name[20];          //学生姓名
            unsigned int num;      //学号
            int ismale;                  //性别
        };                                //这里注意还有一个;
        int main(void)
        {
            struct student s1;   //s1是一个变量,类型是struct student
            //给结构体变量赋值
            s1.name[0]='J';
            s2.name[1]='i';
            s3.name[2]='m';
            s4.name[3]='\0';

            s1.num  = 123 ;
            s1.ismale= 1 ;
            //打印检验
            printf("s1.name = %s,s1.num = %d,s1.ismale = %d.\n",s1.name,s1.num,s1,ismale);
            return 0;
        }

Data structures: the large and complex data organization and management together a certain way, ease of operation (search, add, delete, etc.) This is called data structures.
Relationship between structures and arrays: the array is a special structure, special is that the same type of the respective elements of the package.

To use the structure:
STEP 1: the structure type. Structure type is defined as a function of the outside (outside == global function).
Step 2: Use the first step to define the type of structure variables.
The third step: Use variables. In fact when using the structure of the variable, the variable used is a structure in each sub-element of the package.

Guess you like

Origin blog.51cto.com/14762640/2485550