一、定义结构体类型变量的方法
例题:嵌套定义
#include<stdio.h>
void main(){
struct date{
int month;
int day;
int year;
};
struct {
int num;
char *name;
char sex;
struct date birthday; //嵌套定义。将年月日见嵌套到birthday中
float score;
}boy;
}
#include<stdio.h>
void main(){
struct student{
int num;
char *name;
char sex;
float score;
}boy;
boy.num = 007;
boy.name = "Jane";
printf("The address of struct is %o:\n",&boy); //%o以八进制数形式输出整数,
printf("The address of struct is %o:\n",&boy.num);
}
为什么会这样呢?
原因和数组差不多,变量名指向的是首地址。
二、结构体变量的初始化
#include<stdio.h>
void main(){
struct student{
int num;
char *name;
char sex;
double score;
}boy1,boy2 = {102,"AJ",'M',98.5}; //初始化
boy1 = boy2;
printf(" number = %d\n name = %s\n score = %lf\n",boy1.num,boy1.name,boy1.score);
printf("\n\n");
printf(" number = %d\n name = %s\n score = %lf\n",boy2.num,boy2.name,boy2.score);
}
例题:投票系统
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct toupiao
{
char name[20];
int piaoshu;
}a = {"zsl"}, b = {"亚索"}, c = {"盖伦"},d = {"赵信"} ;
void main()
{
char shuru[20] ;
int i, max, a1=0, b1=0, c1=0, d1=0;
int x[4]={0,0,0,0};
printf("\t\t\t欢迎进入优秀老师投票系统!\n\n\n");
printf("选举候选人:\n\t\t\tzsl,亚索,盖伦,赵信\n");
printf("********************************************************************************\n\n\n");
for ( i=1 ; i<11 ; i++ )
{
printf("第%d位投票,请输入候选人的名字:",i);
gets( shuru );
if( strcmp(shuru,a.name)==0 )
{
x[0]++;
}
else if( strcmp(shuru,b.name)==0 )
{
x[1]++;
}
else if( strcmp(shuru,c.name )==0 )
{
x[2]++;
}
else if( strcmp(shuru,d.name )==0 )
{
x[3]++;
}
}
max = x[0] ;
for( i=1 ; i<4 ; i++ )
{
if(max < x[i])
{
max = x[i];
}
}
printf("********************************************************************************\n");
printf("\t各候选人的票数如下:\n\tzsl: %d 票\n\t亚索: %d 票\n\t盖伦: %d 票\n\t赵信: %d 票\n\n", x[0], x[1], x[2],x[3] );
if( max == x[0] )
{
printf("本次投票的胜利者是: %s \n", a.name);
}
else if( max == x[1] )
{
printf("本次投票的胜利者是: %s \n", b.name );
}
else if( max == x[2] )
{
printf("本次投票的胜利者是: %s \n", c.name );
}
else if( max == x[3] )
{
printf("本次投票的胜利者是: %s \n", d.name );
}
system("pause");
}
二、指向结构体类型数据的指针
例题: 指针
#include<stdio.h>
struct stu{
int num;
char *name;
char sex;
double score;
}boy = {102,"AJ",'M',78.5}; //初始化
void main(){
struct stu *pstu;
pstu = &boy;
printf(" number = %d\n name = %s\n ",boy.num,boy.name);
printf(" number = %d\n name = %s\n ",(*pstu).num,(*pstu).name);
printf(" number = %d\n name = %s\n ",pstu->num,pstu->name);
}
三、结构指针变量作函数参数
例题:
第一种方法:先用结构体变量作函数参数
#include<stdio.h>
#include<string.h>
struct student{
int num;
char name[20]; //char *name;对应下面的注解
float score[3];
};
void print(struct student);
void main(){
struct student stu;
stu.num = 8;
strcpy(stu.name,"baidu.com");//将baidu.com复制给我->stu.name
stu.score[0] = 98.5;
stu.score[1] = 99.5;
stu.score[2] = 95.5;
print(stu);
}
void print(struct student stu){
printf("\tnum :%d\n",stu.num);
printf("\tname :%s\n",stu.name);
printf("\tscore_1 :%5.2f\n",stu.score[0]);
printf("\tscore_2 :%5.2f\n",stu.score[1]);
printf("\tscore_3 :%5.2f\n",stu.score[2]);
}
第二种方法:改用指向结构体变量的指针作实参
#include<stdio.h>
#include<string.h>
struct student{
int num;
char name[20]; //char *name;对应下面的注解
float score[3];
};
void print(struct *p);
void main(){
struct student stu;
stu.num = 8;
strcpy(stu.name,"baidu.com");//将baidu.com复制给我->stu.name
stu.score[0] = 98.5;
stu.score[1] = 99.5;
stu.score[2] = 95.5;
print(&stu);
}
void print(struct student *p){
printf("\tnum :%d\n",p -> num);
printf("\tname :%s\n",p -> name);
printf("\tscore_1 :%5.2f\n",p -> score[0]);
printf("\tscore_2 :%5.2f\n",p -> score[1]);
printf("\tscore_3 :%5.2f\n",p -> score[2]);
printf("\n");
}
四、动态存储分配
数组这样定义是错误的:
char a[n];
数组要提前告知数组长度,而往往所需的内存空间取决于实际输入的数据,所以是无法提前确定数组长度的。
4.1、解决方法:
4.1.1、malloc函数
函数原型为:
void *malloc(unsingned int size);
4.1.2、calloc函数
函数原型为:
void *calloc(unsingned n,unsingned size);
4.1.3、free函数
函数原型为:
void free(void p);
4.2、链表
例题:
#include<stdio.h>
struct student{
long num;
float score;
struct student *next;
};
void main(){
struct student a,b,c,*head; //head是一个指向结构变量的指针
a.num = 10101;
a.score = 89.5;
b.num = 10102;
b.score = 98.5;
c.num = 10103;
c.score = 85;
head = &a; //head必须指向第一个元素
a.next = &b;
b.next = &c;
c.next = NULL;
do{
printf("%1d %5.1f\n",head->num,head->score);
head = head -> next;
}while(head != NULL);
}
五、共用体
5.1、共用体的概念
定义结构体变量时,把struct
关键字换成union
就会定义一个共用体变量。共用体变量中的每个成员共享一个内存地址。
5.2、共用体的引用方式
六、共用体和结构体的比较
例题:
七、枚举
#include<stdio.h>
void main(){
enum weekday{sun,mon,tue,wed,thu,fri,sat}a,b,c;
a = sun;
b = mon;
c = tue;
printf("%d,%d,%d",a,b,c);
printf("\n\n");
}
sun、mon、tue是给人看的;0,1,2是给机器看的
八、typedef 定义类型
1、用typedef
声明新的类型名来代替已有的类项名
2、例如:声明INTEGER为整形
#include<stdio.h>
typedef int INTEGER; //将int替换成了INTEGER
void main(){
INTEGER i = 666;
printf("%d\n",i);
}
3、声明结构类型
#include<stdio.h>
typedef struct{
int month;
int day;
int year;
}DATE;
void main(){
DATE date_one;
date_one.year = 2021;
date_one.month = 8;
date_one.day = 25 ;
printf("%d - %d - %d\n",date_one.year,date_one.month,date_one.day);
}
typedef与define的区别: