结构体

来源:互联网 发布:五黄煞算法口诀 编辑:程序博客网 时间:2024/05/01 06:43

一 结构体 

结构体的定义

 struct student //定义结构提名 

 char name[10]; //成员列表(包括成员类型,和成员名) 

int age ;

 int num;

 float score; 

} ; //特别注意 不要漏掉次“; ”号。

 二 定义结构体变量

 方法一; 

struct student {

 char name[10];

 int age ; 

int num; 

float score;

 }student1,student2;//直接在结构体定义的结尾定义结构体变量 student1,student2. 

方法二 

struct student { 

char name[10];

 int age ;

 int num; 

float score;

} ;

 Int main() {

 struct student student1,student2;//在主函数中定义结构体变量

三 成员也可以是一个结构体(也称结构体嵌套) 

Struct date { 

Int year;

 Int month;

 Int day;

 }; 

struct student { 

char name[10];

int age ; 

int num; 

float score; 

Struct date birthday; //birthday 是一个(struct date)结构类型。

 }student1,student2;

 四 结构体变量的引用

 方法一

 结构题变量名.结构体成员名 * 不要忽略两个之间的小点* 例如:student1.num=10000; *其中的点是分量运算符* 他是所有运算符中优先级最高 的 

例如:student1.birthday.month=6; *这是对结构体嵌套成员引用的方法*

 注意 结构体变量可以像变量一样进行种运算。 也可以通过 scanf 对结构体变量赋值 例如:scanf("%d",student1.num); 

也可以是:scanf("%d%d%f",student.num,student.age,student1.birthday.year); 

五,结构体变量的初始化 

方法一:

 直接在结构体定义的过程中直接赋值 例如:

 Struct student {

 int num;

 Float score;

 Char name[10];

 }student1={10101,78.6,"malong"},student2={10110,88.6,"helu"}; 

Printf("num=%d \nscore=%f\n;name=%s\n",student1.num,student1.score,student1.name); 

方法二:

 在主函数中进行初始化;

 #include<stdio.h>

 #include<string.h>

 struct stu {

 int num;

 char name[10]; 

char sex;

 };

 int main()

 { 

struct stu stu_1,stu_2; //z 

stu_1.num=1,strcpy(stu_1.name,"malong"),stu_1.sex='m'; 

stu_2.num=2,strcpy(stu_2.name,"haohaibo"),stu_2.sex='w';

 printf("num=%d name=%s sex=%c\n",stu_1.num,stu_1.name,stu_1.sex);

return 0;

 } 

总结:在对结构体变量中字符数组成员初始化中,如果采用第二种方法 切记不能指直接 stu_2.name="malong"; 

而是利用字符串函数进行初始化 如上例所示 结构体数组 所谓结构体数组,就是 在定义结构体变量时定义的是一个结构体数组变量;

 格式 struct 结构体名 数组名【大小】 ; 下来举例说明

 #include<stdio.h>

struct date {

 int year;

 int month;

 int day;

 }; 

struct stu { int num; char name[10];

 float score; 

struct date birthday; //birthday 是一个 struct date 类型的结构体变量

 }stu[3]={{1,"malong",78.6,1998,06,01}, // 定义一个结构体变量数组,并对其初始化 

{2,"haohaibo",88.8,1990,6,3}, {3,"helu",88.9,1990,5,2}};

 int main() { 

int i;

 for(i=0;i<3;i++) 

printf("%d %s %4.2f %d %d %d\n",stu[i].num,stu[i].name,stu[i].score,stu[i].birthday.year,stu [i].birthday.month,stu[i].birthday.day); // 对结构体数组成员数组尽享输出,切记这里的 stu[i].num 不能写成 stu.num 原因是,前者是结构体变量,后者是结构里变量的地址// 

} }

 再举一例 加深理解 要求 对候选人得票统计程序。设 3 个候选人,每次输入一个候选人的名字,对应的票数加 1,最后输出每位候选人即其的票数 程序如下: #include<stdio.h> 

#include<string.h>

struct person { char name[20];

 int count;

 }

leader[3]={{"malong",0},{"haohaibo",0},{"helu",0}};

 int main() 

char name[20];

 int i,j,num; 

for(i=0;i<5;i++) {

 scanf("%s",name);

 for(j=0;j<3;j++) {

 if(strcmp(name,leader[j].name)==0) leader[j].count++; 

}

 for(i=0;i<3;i++)

 printf("%s %d\n",leader[i].name,leader[i].count); return 0; }

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

1.指向结构体变量的指针

 指向结构体变量的指针 根据名字自己理解 就是 定义一个结构类型的指针,然后这个指针指向结构体变量 结构体指针的定义方法为:

 Struct 结构体名 *指针变量名; 例如:struct stenden *p;// 这就定义了一个指向结构体 student 的结构体指针

 举例说明: 再次声明 计算机程序中能采用指针则采用指针,因为运算速度快,所以要好好学习指针

 原因很简单:指针是对内存地址的操作,不采用指针的话 则每次要先取地址,再取值

 #include<stdio.h>

 struct stu {

 int num;

 float score; 

};

 int main() {

 struct stu stu_1; 

stu_1.num=1,stu_1.score=99.8;

struct stu *p;

 p=&stu_1; //切记不要忘了指针指向结构比变量的首地址

 printf("%d %4.2f\n",p->num,p->score); //引用

 return 0;

 } 

总计这里的 p->num 等价于 (*p).num 和 stu_1.num;都是结构体变量成员的引用方式

 2.指向结构体变量数组的指针。 

指向结构体变量数组的指针。 指向结构体变量数组的指针 通过名字理解:就是指定义了一个结构体变量数组,然后定义了一个结构体指针变量;

结构 体变量指针指向结构体数组。就这么简单 关键是记住定义方法 后 OK 了。

 #include<stdio.h> 

struct stu {

 int num;

 char name[10];

 float score;

 }

student[3]={{1,"malong",88.9},{2,"haohaibo",99.8},{3,"helu",88.7}}; 量数组,并对其初始化

//定义了一个结构体变

int main()

 { struct stu *p; //定义了一个结构体指针 指针指向为结构体 stu 的首地址 

p=student; // 结构体指针指向结构体变量数组的首地址

 int i;

 for(i=0;i<3;i++) 

{ printf("%d %s %4.2f\n",p->num,p->name,p->score);

 p++; 

}

 } 

关于结构体这部分的综合练习:

 #include<stdio.h>

 #include<string.h> 

struct date { int year; int month; int day; };

 struct stu {

int num; 

char name[10];

 char sex; 

struct date birthday;

 }; 

int main()

 { struct stu student[3]; 

struct stu *p;

 int i; 

p=student;

 p->num=1,strcpy(p->name,"malong"),p->sex='m';

p->birthday.year=1996,p->birthday.month =6,p->birthday.day=5; 

(p+1)->num=2,strcpy((p+1)->name,"liuxiao"),(p+1)->sex='w',(p+1)->birthday.year=1992,(p +1)->birthday.month=4,(p+1)->birthday.day=6; (p+2)->num=3,strcpy((p+2)->name,"haohaibo"),(p+2)->sex='m',(p+2)->birthday.year=1990,( p+2)->birthday.month=3,(p+2)->birthday.day=18; for(i=0;i<3;i++) printf("%-3d %-8s %c %-4d %-2d %-2d\n",(p+i)->num,(p+i)->name,(p +i)->sex,(p+i)->birthday.year,(p+i)->birthday.month,(p+i)->birthday.day); return 0; }

原创粉丝点击