12.结构体

1.概述

1.1定义一个结构的一般形式为:

struct 结构名

{

        成员表列

};

成员列表由若干个成员组成,每个成员都是该结构的一个组成部分。对每个成员也必须作类型说明,其形式为:类型说明符  成员名;

struct student
{
    int num;
    char name[20];
    float score;
};
1.2定义结构类型变量的方法

1.2.1在声明类型的同时定义变量,这种形式的定义一般形式为:

struct 结构名

{

        成员表列

}变量名表列;

struct student
{
    int num;
    char name[20];
    float score;
}student1,student2;

                         在定义了结构体变量之后,系统会为之分配内存单元

1.2.2也可以直接定义结构体类型变量(即不出现结构体名)

struct 

{

        成员表列

}变量名表列;

struct 
{
    int num;
    char name[20];
    float score;
}student1,student2;

2.结构体变量的引用

2.1在定义了结构体变量以后,当然可以引用这个变量。但应遵守以下规则:

 2.1.1 不能将一个结构体变量作为一个整体进行输入和输出。
          例如:打印student的各个变量的值
          Printf(“%d,%s,%c,%d,%f,\n",student1);   //错误写法

2.1.2 正确引用结构体变量中成员的方式为:

                     结构体变量.成员名    

           Student1.num 表示student1变量中的num成员,即student1的num(学号)项,可以对变量的成员赋值,例如: student1.num=100;
“.”是成员运算符,它在所有的运算符中优先级最高,因此可以把 student1.num作为一个整体来看待。上面赋值语句的作用是将整数100赋值给 student1变量中的成员num

参考下面代码理解其正确使用方法:

#include

void main()
{
    struct footballer
    {
        int num;
        char *name;
        char sex;
        int score;
    }boy1,boy2;

    boy1.num = 77;
    boy1.name = "k77";
    boy2.num = 7;
    boy2.name = "cr7";
    
    printf("please input sex and score:");
    
    scanf("%c %d %c %d",&boy1.sex,&boy1.score,&boy2.sex,&boy2.score);

    printf("boy1 number is %d,name is %s,sex is %c,score is %d\n",boy1.num,boy1.name,boy1.sex,boy1.score);

    printf("boy2 number is %d,name is %s,sex is %c,score is %d",boy2.num,boy2.name,boy2.sex,boy2.score);

}
2.2 如果成员本身又属于一个结构体类型,则要用若干个成员运算符,一级一级地找到最低的一级的成员。只能对最低级的成员进行赋值或存取以及运算,例如:
struct date            struct           
{                      {
    int month;             int num;
    int dat;               char name[10];
    int year;              struct date birthday;
};                     }boy1,boy2;

其中,成员birthday被说明为date结构类型,成员名可与程序中其他变量同名,互不干扰

2.3 对于结构体变量的成员可以像普通变量一样进行各种运算(根据其类型决定可以进行的运算)

例如:student2.score = student1.score;

sum = student1.score + student2.score; student.age ++;++student2.age;

2.4可以引用结构体变量成员的地址,也可以引用结构体变量的地址   

但不能用以下语句整体读入结构体变量:

Scanf(“%d,%s,%c,%d,%f,%s",&student1);

结构体变量的地址主要用作函数参数,传递结构体变量的地址

3.结构体变量的初始化


    struct footballer
    {
        int num;
        char *name;
        char sex;
        int score;
    }boy1 = {77,"k77",'m',37},boy2 = {7,"cr7",'m',924};


  

或者

 struct footballer
    {
        int num;
        char *name;
        char sex;
        int score;
    }boy1;

    boy1.num = 77;
    boy1.name = "k77";
    boy1.sex = 'm';
    boy1.score = 37;


  

4.结构体数组

一个结构体变量中可以存放一维数据(如一个学生的学号、姓名、成绩等数据)    
如果有10个学生的数据需要参加运算,显然应该用数组,这就是结构体数组
结构体数组与以前介绍过的数值型数组不同之处在于每个数组元素都是一个结构体类型的数据,它们都分别包含各个成员项

4.1定义结构体数组

     和定义结构体变量方法类似,只需说明其为数组即可


struct person
{
    char name[50];
    char vx[20];
};

   struct person man[3];  //定义结构体数组
    
4.2结构体数组初始化
struct person
{
    char name[50];
    char vx[20];
}man[2] = {
            {"ww","1300709"}, 
            {"rr","1355083"}
        
          };
  
或者
struct person
{
    char name[50];
    char vx[20];
};

  struct person  man[2]={{"ww","3453"}, {"ee","4322"}};

下面代码具体说明:

#include

struct person
{
    char name[50];
    char vx[20];
};
int main()
{
    struct person man[3];
    int i;

    for(i=0;i<3;i++)
    {
        printf("input the name:");

        gets(man[i].name);

        printf("input the vx:");

        gets(man[i].vx);
    }

    for(i=0;i<3;i++)
    {
        printf("name is %s,vx is %s\n",man[i].name,man[i].vx);
    }


    return 0;
}

5.指向结构体类型数据的指针

一个结构体变量的指针就是该结构体变量所占据内存段的起始地址,可以设一个指针变量,用来指向一个结构体变量,此时该指针变量的值是结构体变量的起始地址,指针变量也可以用来指向结构体数组中的元素

5.1结构体指针变量说明的一般形式为:

     struct 结构名*结构体指针变量名

     例如,前面的例题中定义了stu这个结构,如要说明一个指向stu指针变量 pstu,

     可写为:struct stu *pstu;

     当然也可在定义stu结构时同时说明pstu.与前面讨论的各类指针变量相同,结构体指针变量也         必须要先赋值后才能使用

5.2 赋值是把结构体变量的首地址赋予该指针变量,不能把结构名赋予该指针变量

      如果boy 是被说明为stu类型的结构变量,则:

      pstu = &boy; //是正确的
      pstu = &stu; //是错误的

  因为结构名和结构变量是两个不同的概念,不能混淆。结构名只能表示一个结构形式,编译            系统并不对它分配内存空间。只有当某变量被说明为这种类型的结构时,才对该变量分配存            储空间。因此上面&stu 这种写法是错误的,不可能去取一个结构名的首地址。有了结构体  指        针变量,就能更方便地访问结构体变量的各个成员

   

5.3其访问的一般形式为:


         (*结构体指针变量).成员名

或为:
         结构指针变量->成员名
例如:
          (*pstu).num

或者
          pstu ->num

下面用代码来具体说明:

#include
#include

struct stu
{
    int num;
    char *name;
    char sex;
    float score;
}boy1 = {02,"kk",'m',99.5};

void main()
{
    struct stu *p;
    p = &boy1;

    printf("number is %d,name is %s\n",boy1.num,boy1.name);
    printf("sex is %c,score is %f\n",boy1.sex,boy1.score);

    printf("number is %d,name is %s\n",(*p).num,(*p).name);
    printf("sex is %c,score is %f\n",(*p).sex,(*p).score);

    printf("number is %d,name is %s\n",p->num,p->name);
    printf("sex is %c,score is %f\n",p->sex,p->score);
}

6.结构体指针变量作函数参数

将一个结构体变量的值传递给另一个函数,有3个方法:    
1.用结构体变量的成员作参数
2.用结构体变量作实参
3.用指向结构体变量(或数组)的指针作实参,将结构体变量(或数组)的地址传给形参

例如,有一个结构体变量stu,包含学生的学号,姓名和三门课程的成绩,通过函数print输出:

1.用结构体变量作函数参数

#include
#include

struct student
{
    int num;
    char name[20];
    float score[3];
};
void print(struct student);
void main()
{
    struct student stu;

    stu.num = 02;
    strcpy(stu.name,"kk");
    stu.score[0] = 60;
    stu.score[1] = 75.5;
    stu.score[2] = 82;

    print(stu);
}
void print(struct student stu)
{
    printf("num is %d \n",stu.num);
    printf("name is %s \n",stu.name);
    printf("score 1 is %f \n",stu.score[0]);
    printf("score 2 is %f \n",stu.score[1]);
    printf("score 3 is %f \n",stu.score[2]);
}

2.改用指向结构体变量的指针作实参

#include
#include

struct student
{
    int num;
    char name[20];
    float score[3];
};
void print(struct student*);
void main()
{
    struct student stu;

    stu.num = 02;
    strcpy(stu.name,"kk");
    stu.score[0] = 60;
    stu.score[1] = 75.5;
    stu.score[2] = 82;

    print(&stu);
}
void print(struct student *p)
{
    printf("num is %d \n",p->num);
    printf("name is %s \n",p->name);
    printf("score 1 is %f \n",p->score[0]);
    printf("score 2 is %f \n",p->score[1]);
    printf("score 3 is %f \n",p->score[2]);
}


 


 


 



 

你可能感兴趣的:(c语言,c语言)