struct in C language usage

Reference from C language struct usage

C-language structure (struct) with the object-oriented programming (e.g., java, etc.) inside the class is very similar. However, as C ++ for a structure which is extended, inside the structure c ++ methods can comprise, but can not contain the C language which is a method (function).

Structure is composed of basic data types, and the combination with an identifier of named variables.

Structure can be used in different types of data.

1. The structure of structure description and variable definitions

In Turbo C, the structure is a data type, variable structure can be used, therefore, as the other types of variables, its definition when using the first structure variables.

The general format definition structure variable is:

 struct 结构名 

 { 

      类型  变量名; 

      类型  变量名; 

      ... 

 } 结构变量; 

Structure identifier name is not a variable name structure. Type five data types (integer, floating point, character, and non-point type value type).
Each type of variables including the structure known as structural members, it is the same as the elements of the array, but the array elements are accessed less marked, but the structure is based on a variable name to access members.

Here is an example to illustrate how to define the structure variables.

 struct string 

 { 

      char name[8]; 

      int age; 

      char sex[2]; 

      char depart[20]; 

      float wage1, wage2, wage3, wage4, wage5; 

 } person; 

This example defines the structure as a string variable named person, the person if the variable name is omitted, the description of the structure becomes. With the name of the structure has been described structure can also be defined variables. When this definition becomes the Example:

 struct string 

 { 

      char name[8]; 

      int age; 

      char sex[2]; 

      char depart[20]; 

      float wage1, wage2, wage3, wage4, wage5; 

 }; 

 struct string person; 

In this way if convenient structure to define a plurality of variables having the same form, to make it

Structure Description, and then the name of the structure to define variables.

E.g:

 struct string Liming, Liuqi, ...; 

If the structure name is omitted, the structure is called unknown, which is often the case within the function, with the previous example into such a configuration when:

 struct 

 { 

      char name[8]; 

      int age; 

      char sex[2]; 

      char depart[20]; 

      float wage1, wage2, wage3, wage4, wage5; 

 } Liming, Liuqi; 
  1. Use of structural variables

Structure is a new data type, the structure may be assigned as a variable to other types of variables, calculation, except that the structure variables as the basic member variable.

Representation of the structure of members is as follows:
The structure member variable name

If the "structure variables. Member Name" as a whole, then the whole data types and structures

The members of the same data type, such as can be used as stated before variable.

The following example defines a variable configuration, wherein each member of the data received from the keyboard, and

Summation of the floating structure, and display the calculation result, while the data is stored in a text entitled

wage.dat disk file. Please note that the visit of members of different structure in this example.

Example 3:

 #include <stdio.h> 

 main() 

 { 

      struct{                  /*定义一个结构变量*/ 

           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");    

	   /*创建一个文件只写*/ 
      while(c==’Y’||c==’y’)         

/*判断是否继续循环*/ 
      { 
           printf("/nName:"); 
           
           scanf("%s", a.name);     /*输入姓名*/ 
           
           printf("Age:"); 
           scanf("%d", &a.wage);    /*输入年龄*/ 

           printf("Sex:"); 

           scanf("%d", 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); 

           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);/*显示结果*/ 

           fprintf(fp, "%10s%4d%4s%30s%10.2f/n",   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); 

 } 
  1. Structured pointer and the array of structures

Structure is a new data type, it can also be structured pointer arrays and structures.

First, the structure array

Is an array of structures having the same structure as the set of variable type. If you want to define a class 40 student name, gender, age and address, it can be defined as an array of structures. As follows:

 struct{ 

      char name[8]; 

      char sex[2]; 

      int age; 

      char addr[40]; 

 }student[40]; 

Also it is defined as:

 struct string{ 

      char name[8]; 

      char sex[2]; 

      int age; 

      char addr[40]; 

 }; 

 struct string student[40]; 

It should be noted that the visit is a member of the array element array structure variable structure, in the form of:

  结构数组元素.成员名 

E.g:

  student[0].name 

  student[30].age 

In fact a two-dimensional configuration corresponding to an array of structures, the first dimensional structure of array elements, each element

A variable structure, the second dimension is a structural member.

note:

Members of the structure of the array can be an array of variables.

E.g:

 struct a 

 { 

      int m[3][5]; 

      float f; 

      char s[20]; 

 }y[4]; 

To access a structural configuration variable y [2] This variable can be written as

   y[2].m[1][4] 

Second, the structure pointer

Structure pointers pointers to structures. It consists of a plus defined in the "*" operator structure before the variable name, for example, with a defined structure pointer previously described structure is as follows:

 struct string{ 

      char name[8]; 

      char sex[2]; 

      int age; 

      char addr[40]; 

 }*student; 

Name structure pointer may be omitted only described structure, and then use the following statement definition structure pointer.

  struct string *student; 

Using a structured means to access, and structural variables to structural members access to structure members in the expression

On different. Structure refers to access to structural members is expressed as:

   结构指针名->结构成员 

其中"->"是两个符号"-"和">"的组合, 好象一个箭头指向结构成员。例如要 

To the above defined structure name and age assignment, you can use the following statement:

 strcpy(student->name, "Lu G.C"); 

 student->age=18; 

* In fact, student-> name is ( Student) .name of the short form.

It is noted that a structure pointer is a pointer to the structure, i.e., the first address of the first member of the structure, the structure should therefore pointers are initialized, i.e., the assigned byte length of the entire structure of the space prior to use,

This function can be used following completion, the above embodiment will be described still, as follows:

 student=(struct string*)malloc(size of (struct string)); 

size of (struct string) is automatically strike the string length in bytes of the configuration,

malloc () function defines the structure of a memory area of ​​size length, and then returned as a structure pointer address fraud.

note:

  1. Structure-structured pointer variables or variable structure as a data type, and therefore have the same definitions as local variables and global variables, depending on the position of the definition given.

  2. Structure variable name is not at the address of the structure, which is the meaning of an array of different names, so the requirements For the first address of the structure should be the first member of the & [structure variable name].

A nested structure

Refers to a nested structure in the structural member may include a structure other, Turbo C allowed the nesting.

Example: The following is a nested structure

 struct string{ 

      char name[8]; 

      int age; 

      struct addr address; 

 } student; 

Where: addr is the name of the structure to another structure, must first be explained, that is,

 struct addr{ 

      char city[20]; 

      unsigned lon zipcode; 

      char tel[14]; 

 } 

If zipcode give the student assignment structure member address structure can be written as:

  student.address.zipcode=200001; 

Each structural member name from the outermost listed one by one until the innermost layer, i.e., the expression nested structure members

They are:

Structure variable name. Nested structure variable name. Structure Member Name

Where: nested structure can have a lot of structural members were named members of the innermost structure is not a structure.

Second, bit architecture

Bit configuration is a special structure, when a plurality of bits bit access basis having a byte or word, bit configuration

The bitwise operators than easier.

The general form of the structure is defined as position:

 struct位结构名{ 

      数据类型 变量名: 整型常数; 

      数据类型 变量名: 整型常数; 

 } 位结构变量; 

Wherein: the data types must be int (unsigned or signed). Must be non-negative integer constant integer in the range 0 to 15, represents the number of bits, i.e., it indicates how many bits.

Variable names are options, can not be named, this provision is to arrange required.

For example: The following defines a bit configuration.

 struct{ 

      unsigned incon: 8;    /*incon占用低字节 的0~7共8位*/ 

      unsigned txcolor: 4;/*txcolor占用高字节的0~3位共4位*/ 

      unsigned bgcolor: 3;/*bgcolor占用高字节的4~6位共3位*/ 

      unsigned blink: 1;  /*blink占用高字节的第7位*/ 

 }ch; 

Access and the same access structure members of the structure members of bits.

For example: Example bgcolor access member of bit configurations can be written:

  ch.bgcolor 

note:

  1. Bit structure members may be defined as unsigned, Signed also be defined as, but is a member of length 1, it will be considered as unsigned type. Because having a single bit symbols impossible.

  2. Bit structure members can not use arrays and pointers, but can be a bit variable structure arrays and pointers, if the pointer is, the way its members access to the same structure pointer.

  3. The total length of the bit configuration (bits), the number of bits of the respective members are defined and can be more than two bytes.

  4. Bit structure members can be used with other structural members.

E.g:

 struct info{ 

      char name[8]; 

      int age; 

      struct addr address; 

      float pay; 

      unsigned state: 1; 

      unsigned pay: 1; 

      }workers;’ 

The example of the architecture defines information about a worker's wages. There are two-bit structure member, only one member of each bit structure, so only one byte but saved two messages, the first byte indicates the status of workers, the second bit indicates whether the wages are paid . This shows that the use of bit architecture can save storage space.

Add that knowledge: typedef defined structure

E.g:

typedef struct  person {

int age ;

char *name;

char *sex;

} student;

We can use student to define a structure variable: student stu1; the effect is equivalent to typedef struct person you took an alias, if we do not use typedef to define our student definition, this time student is a structure variable, to define the structure variable, then we have to be defined by struct person struct person stu1.

Other definitions of typedef:

typedef char name[51];

If you want to define an array of 51 long more of the same type, we can directly use the name defined such as name sex; we may understand here is not very good, if read knowledge about data types are described in terms of everyone well understood, or often using java program on well understood

In fact, we can put typedef char name [51] seen as typedef char [51] name, according to the syntax of typedef name may be known to char name [51] made an alias.

Guess you like

Origin blog.csdn.net/weixin_43115440/article/details/93486050