[C language] -struct

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).

  • The general format
 struct 结构名 
 { 
      类型  变量名; 
      类型  变量名; 
      ... 
 } 结构变量; 

Type five data types (integer, floating point, character, and non-point type value type).

  • Definition of 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, which first structure description for, 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; 
  • Use of structural variables
结构变量.成员名

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

  • Structure array
    structure having an array is a collection of variables of the same structure 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; 
      int m[3][5]; 
      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:

  结构数组元素.成员名 
  student[0].name 
  student[30].age 
  student[2].m[1][4] 
  • 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; 

Use structure refers to access to structural members and structural variables to access structure members differ in expression. Structure refers to access to structural members is expressed as:

结构指针名->结构成员 
 strcpy(student->name, "Lu G.C"); 
 student->age=18; 

In fact, student-> name is the (student) .name of abbreviations.
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,
which can be used to complete the following functions, still above embodiment will be described as follows:

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

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].
  • 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]; 
 } 
  • Bit configuration
    bit configuration is a special structure, access a plurality of bits in a byte or word basis having, bit structure is more convenient than bitwise operators.
 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.

 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; 

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.
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.

  • typedef definition of the structure
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 multiple types of the same length arrays 51, we can directly use the name defined such as name sex; we may understand here is not very good,
in fact, we can put typedef char name [51] seen as typedef char [51] name, according to the syntax typedef name can be known to be char name [51] to obtain alias.

This article is only for the record, the original from
https://blog.csdn.net/weixin_43115440/article/details/93486050

Guess you like

Origin www.cnblogs.com/fenxiangyuan/p/11829666.html