C语言中的结构体的入门学习教程


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

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

  • Title - 标题
  • Author - 作者
  • Subject - 科目
  • Book ID - 编号

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

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; 

访问结构体成员
要访问结构体的任何成员,我们使用成员访问运算符(.)成员访问运算符是编码作为结构体变量名,并且希望访问结构体部件。使用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;
}

让我们编译和运行上面的程序,这将产生以下结果:

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, "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);
}

让我们编译和运行上面的程序,这将产生以下结果:

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, "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);
}

让我们编译和运行上面的程序,这将产生以下结果:

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:%d\n",Huqinwei.age);

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

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

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

struct test{
    int a[3];
    int b;
};
//对于数组和变量同时存在的情况,有如下定义方法:
    struct test student[3] =   {{{66,77,55},0},
                    {{44,65,33},0},
                    {{46,99,77},0}};
//特别的,可以简化成:
    struct test student[3] =    {{66,77,55,0},
                    {44,65,33,0},
                    {46,99,77,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 : %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));
}

运行结果

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

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

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

//对于“一锤子买卖”,只对最终的结构体变量感兴趣,其中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("%d\n",a.b.c);
    a.sb.c = 22;
    printf("%d\n",a.sb.c);

结果无误。

 


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3