C语言中数组允许定义类型的变量,可容纳相同类型的多个数据项,但结构体在C语言编程中,它允许定义不同种类的数据项可供其他用户定义的数据类型。
结构是用来代表一个记录,假设要跟踪图书馆的书籍。可能要跟踪有关每本书以下属性:
- Title - 标题
- Author - 作者
- Subject - 科目
- Book ID - 编号
定义结构体
定义一个结构体,必须使用结构体的struct语句。该struct语句定义了一个新的数据类型,程序不止一个成员。struct语句的格式是这样的:
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
</div>
结构体(structure)标签是可选的,每个成员的定义是一个正常的变量定义,如 int i; 或 float f; 或任何其他有效的变量的定义。在结构的定义的结尾,最后的分号之前,可以指定一个或多个结构变量,但它是可选的。这里是声明书(Book)的结构方式:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
</div>
访问结构体成员
要访问结构体的任何成员,我们使用成员访问运算符(.)成员访问运算符是编码作为结构体变量名,并且希望访问结构体部件。使用struct关键字来定义结构体类型的变量。以下为例子来解释结构的用法:
#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; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s
", Book1.title);
printf( "Book 1 author : %s
", Book1.author);
printf( "Book 1 subject : %s
", Book1.subject);
printf( "Book 1 book_id : %d
", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s
", Book2.title);
printf( "Book 2 author : %s
", Book2.author);
printf( "Book 2 subject : %s
", Book2.subject);
printf( "Book 2 book_id : %d
", Book2.book_id);
return 0;
}
</div>
让我们编译和运行上面的程序,这将产生以下结果:
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</div>
结构体作为函数参数
可以传递一个结构作为函数的参数,非常类似传递任何其他变量或指针。访问可以象在上面的例子已经访问类似结构变量的方式:
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books book );
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printBook( Book1 );
/* Print Book2 info */
printBook( Book2 );
return 0;
}
void printBook( struct Books book )
{
printf( "Book title : %s
", book.title);
printf( "Book author : %s
", book.author);
printf( "Book subject : %s
", book.subject);
printf( "Book book_id : %d
", book.book_id);
}
</div>
让我们编译和运行上面的程序,这将产生以下结果:
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</div>
指针结构
非常相似定义指针结构,来定义指向任何其他变量,如下所示:
struct Books *struct_yiibaier;</div>
现在,可以存储结构变量的地址在上面定义的指针变量。为了找到一个结构变量的地址,将使用运算符&在结构体的名字之前,如下所示:
struct_yiibaier = &Book1;</div>
访问使用一个指向结构的结构的成员,必须使用 -> 运算符如下:
struct_yiibaier->title;</div>
让我们重新写上面的例子中使用结构指针,希望这将能够让我们更容易地理解概念:
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
}
void printBook( struct Books *book )
{
printf( "Book title : %s
", book->title);
printf( "Book author : %s
", book->author);
printf( "Book subject : %s
", book->subject);
printf( "Book book_id : %d
", book->book_id);
}
</div>
让我们编译和运行上面的程序,这将产生以下结果:
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</div>
位字段
位字段允许数据在一个结构体包装。这是特别有用的,当内存或存储数据非常宝贵。典型的例子:
包装几个对象到一个机器语言。例如1位标志能够压缩长度
读取外部的文件格式 - 非标准的文件格式可以读出。例如: 9位整数。
C语言允许我们通过结构定义:bit 长度的变量之后。例如:
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;
</div>
在这里,packed_struct包含6个成员:四个1位标志s f1..f3, 一个 4 位类型和9位my_int。
C语言自动包装上述位字段尽可能紧凑,条件是字段的最大长度小于或等于计算机的整数字长。如果不是这种情况,那么一些编译器可以允许,而其他将重叠存储在下一个字段的存储器。
指针和数组:
这是永远绕不开的话题,首先是引用:

