May 11, 2021 C
The C array allows you to define variables that store data items of the same type, and structure is another user-defined available data type in C programming that allows you to store different types of data items.
Structures are used to represent a record, and if you want to track the dynamics of books in your library, you might want to track the following properties for each book:
In order to define a structure, you must use a struct statement. The struct statement defines a new data type with multiple members, and the struct statement is formatted 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; O r float f; O r other valid variable definitions. A t the end of the structure definition, before the last sign, you can specify one or more structure variables, which is optional. Here's how to declare the Book structure:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
In order to access members of the structure, we use the member access operator (.). A member access operator is a period between the name of the structure variable and the member of the structure that we want to access. Y ou can use the struct keyword to define variables for structure types. 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 Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700
You can use structures as function parameters, passing parameters in a similar way to other types of variables or pointers. You can use the methods in the example above to access the structure variables:
#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 Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
You can define pointers to structures in a similar way to defining pointers to other type variables, as follows:
struct Books *struct_pointer;
You can now store the address of the structure variable in the pointer variable defined above. In order to find the address of the structure variable, place the operator in front of the structure name, as follows:
struct_pointer = &Book1;
In order to access the members of the structure with pointers to the structure, you must use the --gt; operator, as follows:
struct_pointer->title;
Let's use structure pointers to override the above examples, which will help you understand the concept of structural pointers:
#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 executed, it produces the following results:
Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
Some information is stored without taking up a full byte, but with only a few or one binary bits. F or example, when storing a switch amount, there are only 0 and 1 states, which can be entered in 1 bit. To save storage space and make processing easier, the C language provides a data structure called a "bit domain" or "bit segment."
The so-called "bit domain" is to divide the two bits in a byte into several different regions and explain the number of bits in each region. E ach domain has a domain name that allows you to operate by domain name in your program. This allows several different objects to be represented by a byte of binary bit fields.
Typical examples:
Bit domain definitions are similar to structure definitions in the form of:
struct 位域结构名 { 位域列表 };
The list of bit fields takes the form of:
类型说明符 位域名: 位域长度
For example:
struct bs{ int a:8; int b:2; int c:6; };
Bit field variables are described in the same way as structural variables. Y ou can define the instructions first and then explain them, and define the descriptions or explain them directly. For example:
struct bs{
int a:8;
int b:2;
int c:6;
}data;
Explains that data is a bs variable that takes up two bytes. Where bit domain a is 8 bits, bit domain b is 2 bits, and bit domain c is 6 bits.
Let's 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-bit identifiers f1. f4, a 4-bit type, and a 9-bit my_int.
There are a few explanations for the definition of a bit field:
A bit field must be stored in the same byte and cannot span two bytes. I f there is not enough space left in one byte for another bit domain, the bit domain should be stored from the next cell. Y ou can also intentionally make a bit of a domain start with the next cell. For example:
struct bs{ unsigned a:4; unsigned :4; /* 空域 */ unsigned b:4; /* 从下一单元开始存放 */ unsigned c:4 }
In this bit field definition, a is 4 bits of the first byte, the last 4 bits fill 0 means no use, b starts with the second byte, takes 4 bits, and c takes up 4 bits.
A bit field can be a nameless bit field, which is used only for padding or reseeding. A bit field without a name is not available. For example:
struct k{ int a:1; int :2; /* 该 2 位不能使用 */ int b:3; int c:2; };
From the above analysis, it can be seen that bit fields are essentially a type of structure, but their members are assigned in two bits.
Bit domains are used in the same way as structure members, in the general form of:
位域变量名·位域名
Bit fields allow output in a variety of formats.
Take a 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 bit-domain structure bs is defined in the above-example program, and the three bit domains are a, b, and c. E xplains the bs-type variable bit and the pointer variable pbit to the bs type. This means that bit fields can also use pointers.