C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C语言中的结构体的入门学习教程大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

C语言中数组允许定义类型的变量,可容纳相同类型的多个数据项,但结构体在C语言编程中,它允许定义不同种类的数据项可供其他用户定义的数据类型。

结构是用来代表一个记录,假设要跟踪图书馆的书籍。可能要跟踪有关每本书以下属性

定义结构体@H_673_16@ 定义一个结构体,必须使用结构体的struct语句。该struct语句定义了一个新的数据类型,程序不止一个成员。struct语句的格式是这样的

@H_801_20@ struct [structure tag] { member deFinition; member deFinition; ... member deFinition; } [one or more structure variables];

结构体(structurE)标签是可选的,每个成员的定义是一个正常的变量定义,如 int i; 或 float f; 或任何其他有效的变量的定义。在结构的定义的结尾,最后的分号之前,可以指定一个或多个结构变量,但它是可选的。这里是声明书(Book)的结构方式:

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

访问结构体成员@H_673_16@ 要访问结构体的任何成员,我们使用成员访问运算符(.)成员访问运算符是编码作为结构体变量名,并且希望访问结构体部件。使用struct关键字来定义结构体类型的变量。以下为例子来解释结构的用法

@H_801_20@ #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

结构体作为函数参数@H_673_16@ 可以传递一个结构作为函数的参数,非常类似传递任何其他变量或指针。访问可以象在上面的例子已经访问类似结构变量的方式:

@H_801_20@ #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

指针结构@H_673_16@ 非常相似定义指针结构,来定义指向任何其他变量,如下所示:

@H_801_20@ struct Books *struct_yiibaier;

现在,可以存储结构变量的地址在上面定义的指针变量。为了找到一个结构变量的地址,将使用运算符&在结构体的名字之前,如下所示:

@H_801_20@ struct_yiibaier = &Book1;

访问使用一个指向结构的结构的成员,必须使用  -> 运算符如下:

@H_801_20@ struct_yiibaier->title;

让我们重新写上面的例子中使用结构指针,希望这将能够让我们更容易地理解概念:

@H_801_20@ #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

位字段@H_673_16@ 位字段允许数据在一个结构体包装。这是特别有用的,当内存或存储数据非常宝贵。典型的例子:

包装几个对象到一个机器语言。例如1位标志能够压缩长度

读取外部的文件格式 - 非标准的文件格式可以读出。例如: 9位整数。

C语言允许我们通过结构定义:bit 长度的变量之后。例如:

@H_801_20@ 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语言自动包装上述位字段尽可能紧凑,条件是字段的最大长度小于或等于计算机的整数字长。如果不是这种情况,那么一些编译器可以允许,而其他将重叠存储在下一个字段的存储器。

指针和数组:@H_673_16@ 这是永远绕不开的话题,首先是引用:@H_673_16@

@H_801_20@ struct stuff *ref = &Huqinwei; ref->age = 100; printf("age is:%d\n",Huqinwei.agE);

打印可见变化 @H_673_16@ 指针也是一样的@H_673_16@

@H_801_20@ struct stuff *ptr; ptr->age = 200; printf("age is:%d\n",Huqinwei.agE);

结构体也不能免俗,必须有数组:@H_673_16@

@H_801_20@ 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}};

变长结构体:@H_673_16@ 可以变长的数组@H_673_16@

@H_801_20@ #include <stdio.h> #include <malloc.h> #include <String.h> typedef struct changeable{ int iCnt; char pc[0]; }schangeable; main(){ printf("size of struct changeable : %d\n",sizeof(schangeablE)); schangeable *pchangeable = (schangeable *)malloc(sizeof(schangeablE) + 10*sizeof(char)); printf("size of pchangeable : %d\n",sizeof(pchangeablE)); schangeable *pchangeable2 = (schangeable *)malloc(sizeof(schangeablE) + 20*sizeof(char)); pchangeable2->iCnt = 20; printf("pchangeable2->iCnt : %d\n",pchangeable2->iCnt); strncpy(pchangeable2->pc,"Hello world",11); printf("%s\n",pchangeable2->pc); printf("size of pchangeable2 : %d\n",sizeof(pchangeable2)); }

运行结果@H_673_16@

size of struct changeable : 4
size of pchangeable : 4
pchangeable2->iCnt : 20
Hello world
size of pchangeable2 : 4

结构体本身长度就是一个int长度(这个int值通常只为了表示后边的数组长度),后边的数组长度不计算在内,但是该数组可以直接使用。@H_673_16@ (说后边是个指针吧?指针也占长度!这个是不占的!原理很简单,这个东西完全是数组后边的尾巴,malloc开辟的是一片连续空间。其实这不应该算一个机制,感觉应该更像一个技巧吧)

结构体嵌套:@H_673_16@ 结构体嵌套其实没有太意外的东西,只要遵循一定规律即可:@H_673_16@

@H_801_20@ //对于“一锤子买卖”,只对最终的结构体变量感兴趣,其中a、B也可删,不过最好带着 struct A{ struct B{ int c; } b; } a; //使用如下方式访问: a.b.c = 10;

特别的,可以一边定义结构体B,一边就使用上:@H_673_16@

@H_801_20@ struct A{ struct B{ int c; }b; struct B sb; }a;

使用方法与测试:@H_673_16@

    a.b.c = 11;
    printf("%d\n",a.b.c);
    a.sb.c = 22;
    printf("%d\n",a.sb.c);

结果无误。

 

大佬总结

以上是大佬教程为你收集整理的C语言中的结构体的入门学习教程全部内容,希望文章能够帮你解决C语言中的结构体的入门学习教程所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。