C structure


C arrays allow you to define variables that can store data items of the same type. Structure is another user-defined data type available in C programming, which allows you to store different type of data item. The

structure is used to represent a record. Suppose you want to track the dynamics of books in the library. You may need to track the following attributes of each book:

  • Title

  • Author

  • Subject

  • Book ID

Define a structure

In order to define a structure, you must use the struct statement. The struct statement defines a new data type containing multiple members. The format of the struct statement is as follows:

struct [structure tag]{
   member definition;
   member definition;   ...
   member definition;} [one or more structure variables];

structure tag is optional, and each member definition is a standard variable definition. , such as int i; or float f; or other valid variable definitions. At the end of the structure definition, before the last semicolon, you can optionally specify one or more structure variables. Here's how to declare a Book structure:

struct Books{   char  title[50];   char  author[50];   char  subject[100];   int   book_id;} book;

Accessing structure members

To access the members of a structure, we use the member access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access. You can use the struct keyword to define variables of structure type. The following example demonstrates the use of structures:

#include <stdio.h>#include <string.h> struct Books{   char  title[50];   char  author[50];   char  subject[100];   int   book_id;}; int main( ){   struct Books Book1;        /* 声明 Book1,类型为 Book */   struct Books Book2;        /* 声明 Book2,类型为 Book */ 
   /* Book1 详述 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");   Book1.book_id = 6495407;   /* Book2 详述 */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");   Book2.book_id = 6495700; 
   /* 输出 Book1 信息 */
   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);   /* 输出 Book2 信息 */
   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 book_id : %d\n", Book2.book_id);   return 0;}

When the above code is compiled and executed, it produces the following results:

Book 1 title : C ProgrammingBook 1 author : Nuha AliBook 1 subject : C Programming TutorialBook 1 book_id : 6495407Book 2 title : Telecom BillingBook 2 author : Zara AliBook 2 subject : Telecom Billing TutorialBook 2 book_id : 6495700

Structures as function parameters

You Structures can be used as function parameters, and the parameter passing method is similar to other types of variables or pointers. You can access structure variables in the same way as in the above example:

#include <stdio.h>#include <string.h> struct Books{   char  title[50];   char  author[50];   char  subject[100];   int   book_id;};/* 函数声明 */void printBook( struct Books book );int main( ){   struct Books Book1;        /* 声明 Book1,类型为 Book */   struct Books Book2;        /* 声明 Book2,类型为 Book */ 
   /* Book1 详述 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");   Book1.book_id = 6495407;   /* Book2 详述 */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");   Book2.book_id = 6495700; 
   /* 输出 Book1 信息 */
   printBook( Book1 );   /* 输出 Book2 信息 */
   printBook( Book2 );   return 0;}void printBook( struct Books book ){
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);}

When the above code is compiled and executed, it produces the following results:

Book title : C ProgrammingBook author : Nuha AliBook subject : C Programming TutorialBook book_id : 6495407Book title : Telecom BillingBook author : Zara AliBook subject : Telecom Billing TutorialBook book_id : 6495700

Pointer to structure

You can define a pointer to a structure in a similar way as you define a pointer to a variable of other types, as shown below:

struct Books *struct_pointer;

Now you can store the address of the structure variable in the pointer variable defined above. To find the address of a structure variable, put the & operator in front of the structure name, as follows:

struct_pointer = &Book1;

To access a member of a structure using a pointer to the structure, you must use the -> operator , as shown below:

struct_pointer->title;

Let us rewrite the above example using structure pointer, which will help you understand the concept of structure pointer:

#include <stdio.h>#include <string.h> struct Books{   char  title[50];   char  author[50];   char  subject[100];   int   book_id;};/* 函数声明 */void printBook( struct Books *book );int main( ){   struct Books Book1;        /* 声明 Book1,类型为 Book */   struct Books Book2;        /* 声明 Book2,类型为 Book */ 
   /* Book1 详述 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");   Book1.book_id = 6495407;   /* Book2 详述 */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");   Book2.book_id = 6495700; 
   /* 通过传 Book1 的地址来输出 Book1 信息 */
   printBook( &Book1 );   /* 通过传 Book2 的地址来输出 Book2 信息 */
   printBook( &Book2 );   return 0;}void printBook( struct Books *book ){
   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);}

When the above code is compiled and When executed, it will produce the following results:

Book title : C ProgrammingBook author : Nuha AliBook subject : C Programming TutorialBook book_id : 6495407Book title : Telecom BillingBook author : Zara AliBook subject : Telecom Billing TutorialBook book_id : 6495700

Bit field

When some information is stored, it does not need to occupy a complete byte, but only a few or one binary bits . For example, when storing a switch value, there are only two states: 0 and 1, so a 1-bit binary can be used. In order to save storage space and make processing easy, C language provides a data structure called "bit field" or "bit segment".

The so-called "bit field" is to divide the binary bits in a byte into several different areas and specify the number of bits in each area. Each domain has a domain name that allows operations by domain name in the program. In this way, several different objects can be represented by a binary bit field of one byte.

Typical example:

  • When a 1-bit binary is used to store a switch value, there are only two states: 0 and 1.

  • Read external file formats - can read non-standard file formats. For example: 9-digit integer.

Definition of bit fields and description of bit field variables

Bit field definition is similar to structure definition, its form is:

    struct 位域结构名        { 位域列表 };

The form of bit field list For example:

    类型说明符 位域名: 位域长度

For example:

struct bs{    int a:8;    int b:2;    int c:6;};

The description of bit field variables is the same as the description of structure variables. You can define first and then explain, define and explain at the same time, or explain directly. For example:

struct bs{    int a:8;    int b:2;    int c:6;}data;

Description data is a bs variable, occupying two bytes in total. Bit field a occupies 8 bits, bit field b occupies 2 bits, and bit field c occupies 6 bits.

Let us look at another example:

struct packed_struct {  unsigned int f1:1;  unsigned int f2:1;  unsigned int f3:1;  unsigned int f4:1;  unsigned int type:4;  unsigned int my_int:9;} pack;

Here, packed_struct contains 6 members: four 1-digit identifiers f1..f4, a 4-digit type and a 9 bits of my_int.

There are several explanations for the definition of bit fields:

  • A bit field must be stored in the same byte and cannot span Two bytes. If the remaining space of one byte is not enough to store another bit field, the bit field should be stored from the next unit. You can also intentionally start a bit field from the next unit. For example:

    struct bs{    unsigned a:4;    unsigned  :4;    /* 空域 */    unsigned b:4;    /* 从下一单元开始存放 */    unsigned c:4}

    In this bit field definition, a occupies 4 bits of the first byte, and the last 4 bits are filled with 0 to indicate that they are not used. b starts from the second byte and occupies 4 bits. c occupies 4 bits. Bit.

  • Since the bit field is not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte, which means it cannot exceed 8 binary bits. Some compilers may allow memory for fields to overlap if the maximum length is greater than the computer's integer length, and others may store the portion larger than one field in the next word.

  • The bit field can be an unnamed bit field, in which case it is only used for filling or position adjustment. Unnamed bit fields cannot be used. For example:

    struct k{    int a:1;    int  :2;    /* 该 2 位不能使用 */    int b:3;    int c:2;};

As can be seen from the above analysis, the bit field is essentially a structure type, but its members are allocated in binary terms.

Use of bit fields

The use of bit fields is the same as the use of structure members. Its general form is:

    位域变量名·位域名

Bit fields allow output in various formats.

Please look at the following example:

main(){    struct bs{        unsigned a:1;        unsigned b:3;        unsigned c:4;    } bit,*pbit;
    bit.a=1;/* 给位域赋值(应注意赋值不能超过该位域的允许范围) */
    bit.b=7;/* 给位域赋值(应注意赋值不能超过该位域的允许范围) */
    bit.c=15;/* 给位域赋值(应注意赋值不能超过该位域的允许范围) */
    printf("%d,%d,%d\n",bit.a,bit.b,bit.c);/* 以整型量格式输出三个域的内容 */
    pbit=&bit;/* 把位域变量 bit 的地址送给指针变量 pbit */
    pbit->a=0;/* 用指针方式给位域 a 重新赋值,赋为 0 */
    pbit->b&=3;/* 使用了复合的位运算符 "&=",相当于:pbit->b=pbit->b&3,位域 b 中原有值为 7,与 3 作按位与运算的结果为 3(111&011=011,十进制值为 3) */
    pbit->c|=1;/* 使用了复合位运算符"|=",相当于:pbit->c=pbit->c|1,其结果为 15 */
    printf("%d,%d,%d\n",pbit->a,pbit->b,pbit->c);/* 用指针方式输出了这三个域的值 */}

The above example program defines the bit field structure bs, and the three bit fields are a, b, and c. Describes the variable bit of type bs and the pointer variable pbit pointing to type bs. This means that bitfields can also use pointers.