C语言中的结构体的入门学习教程
C语言中数组允许定义类型的变量,可容纳相同类型的多个数据项,但结构体在C语言编程中,它允许定义不同种类的数据项可供其他用户定义的数据类型。 结构是用来代表一个记录,假设要跟踪图书馆的书籍。可能要跟踪有关每本书以下属性:
定义结构体 struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables]; 结构体(structure)标签是可选的,每个成员的定义是一个正常的变量定义,如 int i; 或 float f; 或任何其他有效的变量的定义。在结构的定义的结尾,最后的分号之前,可以指定一个或多个结构变量,但它是可选的。这里是声明书(Book)的结构方式: struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book; 访问结构体成员 #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; } 让我们编译和运行上面的程序,这将产生以下结果: 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 结构体作为函数参数 #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,"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); } 让我们编译和运行上面的程序,这将产生以下结果: 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 指针结构 struct Books *struct_yiibaier; 现在,可以存储结构变量的地址在上面定义的指针变量。为了找到一个结构变量的地址,将使用运算符&在结构体的名字之前,如下所示: struct_yiibaier = &Book1; 访问使用一个指向结构的结构的成员,必须使用 -> 运算符如下: struct_yiibaier->title; 让我们重新写上面的例子中使用结构指针,希望这将能够让我们更容易地理解概念: #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,"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); } 让我们编译和运行上面的程序,这将产生以下结果: 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 位字段 包装几个对象到一个机器语言。例如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; 在这里,packed_struct包含6个成员:四个1位标志s f1..f3,一个 4 位类型和9位my_int。 C语言自动包装上述位字段尽可能紧凑,条件是字段的最大长度小于或等于计算机的整数字长。如果不是这种情况,那么一些编译器可以允许,而其他将重叠存储在下一个字段的存储器。 指针和数组: struct stuff *ref = &Huqinwei; ref->age = 100; printf("age is:%dn",Huqinwei.age); 打印可见变化 struct stuff *ptr; ptr->age = 200; printf("age is:%dn",Huqinwei.age); 结构体也不能免俗,必须有数组: struct test{ int a[3]; int b; }; //对于数组和变量同时存在的情况,有如下定义方法: struct test student[3] = {{{66,77,55},0},{{44,65,33},{{46,99,77},0}}; //特别的,可以简化成: struct test student[3] = {{66,55,{44,33, {46,0}}; 变长结构体: #include <stdio.h> #include <malloc.h> #include <string.h> typedef struct changeable{ int iCnt; char pc[0]; }schangeable; main(){ printf("size of struct changeable : %dn",sizeof(schangeable)); schangeable *pchangeable = (schangeable *)malloc(sizeof(schangeable) + 10*sizeof(char)); printf("size of pchangeable : %dn",sizeof(pchangeable)); schangeable *pchangeable2 = (schangeable *)malloc(sizeof(schangeable) + 20*sizeof(char)); pchangeable2->iCnt = 20; printf("pchangeable2->iCnt : %dn",pchangeable2->iCnt); strncpy(pchangeable2->pc,"hello world",11); printf("%sn",pchangeable2->pc); printf("size of pchangeable2 : %dn",sizeof(pchangeable2)); } 运行结果 size of struct changeable : 4 size of pchangeable : 4 pchangeable2->iCnt : 20 hello world size of pchangeable2 : 4 结构体本身长度就是一个int长度(这个int值通常只为了表示后边的数组长度),后边的数组长度不计算在内,但是该数组可以直接使用。 结构体嵌套: //对于“一锤子买卖”,只对最终的结构体变量感兴趣,其中A、B也可删,不过最好带着 struct A{ struct B{ int c; } b; } a; //使用如下方式访问: a.b.c = 10; 特别的,可以一边定义结构体B,一边就使用上: struct A{ struct B{ int c; }b; struct B sb; }a; 使用方法与测试: a.b.c = 11; printf("%dn",a.b.c); a.sb.c = 22; printf("%dn",a.sb.c); 结果无误。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |