——黑马程序员——C语言中构造类型—结构体(二)

来源:互联网 发布:小野丽莎 知乎 编辑:程序博客网 时间:2024/04/29 13:55

-----------android培训java培训、java学习型技术博客、期待与您交流!-----------

结构体数组和结构体指针

一、基本概念

结构体数组:结构体数组的每一个元素都是具有相同结构类型的下标结构变量,可以用结构数组表示具有相同数据结构的一个群体,如一个班的学生档案,一个车间职工的工资标准

一般形式:

struct 结构名

{

成员表列

}数组名[数组长度];

二、结构体数组的定义

1、定义结构体的同时定义数组

struct student 

{

int num;

char *name;

int age;

float score;

}stu1[5]

2、先定义结构体,后定义数组

struct student 

{

int num;

char *name;

int age;

float score;

}

定义结构体数组

struct student stu1[5];

三、结构数组的初始化

1、定义结构体数组同时进行初始化

struct student 

{

int age;

char *name;

}stu1[3] = {{18,"张三"},{28,"李四"},{38,"王五"}};

2、定义同时进行初始化

struct student stu2[2] ={{8,"张三"},{9,"李四"},{10,"王五"}} ;

3、先定义后初始化,整体赋值

struct student stu3[2];

stu3[0] =  (struct student ){16,"张三"};

stu3[1] =  (struct student ){16,"李四"};

次数需要进行类型转换,因为系统 

4、先定义后初始化

struct student sut4[2];

stu[0].age = 17;

stu[0].name = "张三";

当结构体变量中成员 name 定义为char name[21];时可以有一下两种赋值方法

strcpy (stu[0].name, "张三");

scanf("%s",stu[0].name);

四、结构体数组的遍历

利用循环可以遍历结构体数组

#include <stdio.h>#include <stdlib.h>int main(){//定义一个结构体数组    struct student    {int age;        char *name;    }stu1[2] = {{18,"张三"},{19,"李四"}};    int i;    //遍历结构体数组    for(i = 0; i < 2; i++)    {printf("年龄:%d姓名:%s\n",stu1[i].age,stu1[i].name);    }system("pause");return 0;}

五、结构体数组的应用——简易通讯录

通讯录的代码实现#include <stdio.h>#include <stdlib.h>#define LEN 5//1、先定义结构体struct person{//定义姓名char name[21];//保存号码char num[20];};int main (){//2、定义结构体数组struct person contacts[LEN];//3、让用户输入要保存的联系人printf("请输入要保存的信息,格式为:姓名,电话\n");for (int i = 0; i<LEN;i++){scanf("%s %s",contacts[i].name,contacts[i].num);}//4、保存后打印出查看for(int i = 0; i < LEN;i++){printf("姓名:%s 电话:%s",contacts[j].name,contacts[j].num);}return 0;}

六、结构体指针

概念:指向结构体变量的指针,指针指向了结构体的首地址

一般形式:

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

struct student *pstu = NULL;

结构体指针的定义

定义结构体

 struct student 

{

int num;

int age;

}stu;

定义结构体指针

struct   student  *p = NULL;

p = &stu; //让结构体指针指向结构体变量stu

注意:定义结构体指针的同时初始化,如果不初始化会出现野指针,造成内存泄露

七、结构体指针间接访问成员值

一般形式:

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

或者为:

结构指针变量->成员名

#include <stdio.h>#include <stdlib.h>int main(){//定义结构体    struct student     {        int age;        char *name;     //定义结构体变量stu并且初始化       }stu = {18,"张三"};    //定义结构体指针并指向结构体变量stu    struct student *p = &stu;    //用结构体指针访问成员    //(*p).age;   // (*p).name;      // 第二种方法,p是指针时才可以这么写    // p->age;     //p->name;      printf("%d %s\n",(*p).age,(*p).name);     printf("%d %s\n",p->age, p->name);       system("pause");return 0;}

八、结构体的嵌套使用

注意:结构体不可以嵌套自己的变量,可以嵌套自己的指针

如:

//定义一个日期的结构体

struct date

{

int year;

int month;

int day;

};

//定义一个学生结构体

struct student

{

int num;

char *name;

//birthday 是结构体类型的 

struct date birthday;

float score;

};

嵌套的结构体初始化

struct student stu = {{38,"张三",{1999,9,9},80f}

#include <stdio.h>#include <stdlib.h>//定义一个date的结构体struct date{int year;int month;int day;};int main(){//定义一个学生结构体struct student{int num;char *name;//birthday 是结构体类型的 struct date birthday;float score;};//嵌套的结构体初始化struct student stu = {38,"张三",{1999,9,9},80.2f};//访问结构体变量的成员值printf("学号:%d  姓名:%s  出生日期:%d-%02d-%02d  成绩:%f\n",stu.num,stu.name,stu.birthday.year,stu.birthday.month,stu.birthday.day,stu.score);system("pause");return 0;}

结构体嵌套自己的指针

#include <stdio.h>#include <stdlib.h>int main(){//定义结构体    struct person     {char *name;        int age;        //嵌套一个结构体的指针        struct person *child;    };    //定义另一个结构体变量ch    struct person ch = {"张小三",8,NULL};    //变量初始化    struct person per = {"张三",30,&ch};  /*    //也可以这样初始化      per.name = "张三";      per.age = 30;      per.child = &ch;    */    //结构退嵌套自身指针时成员的访问    printf("%s 今年 %d 岁, %s 的儿子是:%s,%s的年龄是:%d岁\n",per.name,per.age,per.name,per.child->name,per.child->name,
<span style="white-space:pre"></span>per.child->age,per.child->age);    system("pause");return 0;}

结构退嵌套自身指针时成员的访问也可以写成这样:
    printf("%s 今年 %d 岁, %s 的儿子是:%s, %s的年龄是:%d岁\n",per.name, per.age, per.name,  ( * per.child).name, ( * per.child).name,  ( * per.child).age,( * per.child).age);

#include <stdio.h>#include <stdlib.h>int main(){//定义结构体    struct person     {char *name;        int age;        //嵌套一个结构体的指针        struct person *child;    };    //定义另一个结构体变量ch    struct person ch = {"张小三",8,NULL};    //变量初始化    struct person per = {"张三",30,&ch};  /*    //也可以这样初始化      per.name = "张三";      per.age = 30;      per.child = &ch;    */    //结构退嵌套自身指针时成员的访问    printf("%s 今年 %d 岁, %s 的儿子是:%s,%s的年龄是:%d岁\n", per.name, per.age, per.name, ( * per.child).name, ( * per.child).name, ( * per.child).age, ( * per.child).age);    system("pause");return 0;}


 总结:此处的*(per.child).name 中必须这样写,* 必须位于口号内,否则报错,它等价于   ( * (per.child ) )



1 0
原创粉丝点击