Latest web development tutorials

C structure

C allows you to define an array of the same type can be stored in a variable data items, thestructure is the C programming another user-defined data types available, which allows you to store different types of data items.

Structure used to represent a record, suppose you want to track dynamic library books, you may need to keep track of each book the following properties:

  • Title
  • Author
  • Subject
  • Book ID

Definition structure

In order to define the structure, you must use thestruct statement.struct statement defines a new data type that contains a plurality of members, the format struct statement is as follows:

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

structure tag is optional, every member definition is the standard definition of a variable, such as int i; or float f; or other valid variable definitions.At the end of the structure defined, the last one before the semicolon, you can specify one or more structural variables, which is optional. Here is the structure declaration Book by:

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

Access to structure member

In order to access members of the structure, we usethe member access operator (.).Member access operator is a period structure variable names and structures of our members want to access between. You can define the type of a variable structure usingthe struct keyword.The following example demonstrates the use of the structure:

#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

Structure as a function parameter

You can put the structure as a function parameter, the Senate manner similar to other types of variables or pointers. You can use the above example the way to access 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

Pointer to a structure

You can define a pointer to the structure, the way the definition of links to other similar types of pointer variable, as follows:

struct Books *struct_pointer;

Now you can address storage structure variables in the above definition of a pointer variable. To find the address of the structure variable, the & operator in front of the name of the structure, as follows:

struct_pointer = &Book1;

To use the pointer to access the member structure points to the structure, you must use the -> operator, as follows:

struct_pointer->title;

Let's use the structure pointer to rewrite the above example, it will help you understand the concept of structure 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

Bit field

Some information is stored and does not need to take a full byte, while accounting for only one or a few bits. For example, when storing a switch, only two states 0 and 1, with a binary bit. To save storage space, and the process is simple, C language and provides a data structure called "bit field" or "bit segment."

The so-called "bit field" is one byte in the binary divided into several different areas, and indicate the number of bits in each region. Each domain has a domain name, allowing the program to operate according to the domain name. So we can put several different objects with a byte binary bit field to represent.

Typical examples:

  • When storing a switch with a binary, only two states 0 and 1.
  • Read external file formats - can be read non-standard file formats. For example: 9-bit integer.

Description Defines bit field variables and a bit field

Bit field definitions and structure definitions similar to the form:

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

Which form bit field list is:

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

E.g:

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

In the same manner described bitfield variables and structure variables described. Can be used after the first definition describes and defines instructions either directly illustrate this in three ways. E.g:

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

Description for bs variable data, a total of two bytes. Which accounted for 8 bit field a bit field b accounted for two, accounting for 6 bit field c.

Let's look at an 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 includes six members: four 1-bit identifier f1..f4, a 4-bit type and a 9-bit my_int.

For the definition of the bit field there are the following instructions:

  • A bit field must be stored in a single byte, not span two bytes. Such as when a byte is not enough space left to store another domain, should be the next storage unit from the bit field. It can also intentionally Reviewed domain from the next unit. E.g:

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

    In this bit field definition, a four bits of the first byte after four said they did not use to fill 0, b from the second byte, occupying 4, c occupies four.

  • Since the bit field is not allowed across the two bytes, so the length of the bit field can not be larger than one byte length, that is not more than 8 binary. If the maximum length is greater than the whole computer word length, some compilers may allow memory overlap region, and some compilers may put more than a portion of the storage domain of the next word.
  • Bit fields can be unnamed bit field, then it is only used as fill or adjustment position. Unnamed bit field can not be used. E.g:

    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 type of structure, but its members are by binary distribution.

Use bit fields

Members that use the same structure and bit field, its general form:

    位域变量名·位域名

Bit field allows a variety of formats.

Consider the following examples:

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 example program defines the bit field structure bs, three-bit field is a, b, c. Description of the type of variable bit bs and pointing bs type pointer variable pbit. This represents a bit field can also be used pointers.