这里写目录标题
结构体变量的引用
机构体变量的初始化
结构体数组
例子
通讯录
#include <stdio.h>
#include <string.h>
#define I 3
struct vote
{
char name[20];
int num;
};
void main()
{
struct vote leader[I]={
"张三",0,"李四",0,"王小二",0};
char leader_name[20];
int i,j;
for(i=1;i<=10;i++)
{
printf("请输入第%d位投票人员: ",i);
scanf("%s",&leader_name);
for(j=0;j<I;j++)
{
if(strcmp(leader_name,leader[j].name)==0)
{
leader[j].num++;
}
}
}
printf("投票结果: \n");
for(i=0;i<3;i++)
{
printf("%s 的票数为 %d\n",leader[i].name,leader[i].num);
}
}
`
结构指针作为函数参数
结构体变量作为函数参数
指向结构体变量的指针作为实参
动态存储分配
动态存储分配
链表
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student) // student结构的大小
struct student *creat(); //创建链表
void print(struct student *head); //打印链表
struct student
{
int num;
float score;
struct student *next;
};
int n; //全局变量,用来记录存放了多少数据。
void main()
{
struct student *stu;
stu = creat();
print( stu );
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN); //LEN是student结构的大小
printf("Please enter the num :");
scanf("%d",&p1->num);
printf("Please enter the score :");
scanf("%f",&p1->score);
head = NULL;
n = 0;
while( p1->num )
{
n++;
if( 1==n )
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nPlease enter the num :");
scanf("%d",&p1->num);
printf("Please enter the score :");
scanf("%f",&p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("\nThere are %d records!\n",n);
p = head;
if( head ) /* 也可以写成 NULL != head */
{
do
{
printf("学号为 %d 的成绩是: %f\n",p->num,p->score);
p = p->next;
}while( p );
}
}
对链表的删除操作
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student) // student结构的大小
struct student *creat(); //创建链表
struct student *del( struct student *head,int num); // del函数用于删除节点, *head即链表
// 的头指针, num是要删除的节点num。
void print(struct student *head); //打印链表
struct student
{
int num;
float score;
struct student *next;
};
int n; //全局变量,用来记录存放了多少数据。
void main()
{
struct student *stu;
stu = creat();
print( stu );
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN); //LEN是student结构的大小
printf("Please enter the num :");
scanf("%d",&p1->num);
printf("Please enter the score :");
scanf("%f",&p1->score);
head = NULL;
n = 0;
while( p1->num )
{
n++;
if( 1==n )
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nPlease enter the num :");
scanf("%d",&p1->num);
printf("Please enter the score :");
scanf("%f",&p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("\nThere are %d records!\n",n);
p = head;
if( head )
{
do
{
printf("学号为 %d 的成绩是: %f\n",p->num,p->score);
p = p->next;
}while( p );
}
}
struct student *del( struct student *head,int num)
{
struct student *p1,*p2;
if( NULL == head )
{
printf("\nThis list is null!\n");
goto end;
}
p1 = head;
while(p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if( num == p1->num )
{
if( p1 == head ) // 当将要删除的结点位于头结点的时候
{
head = p1->next;
}
else // 一般情况
{
p2->next = p1->next;
}
printf("\nDelete No: %d successd!\n",num);
n = n-1;
}
else
{
printf("%d not been found!\n",num);
}
end :
return head;
}
对链表进行插入操作
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student * creat(void)
{
struct student *head;
struct student *p1,*p2;
head = NULL;
n=0;
p1 = p2 = (struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num != 0)
{
n=n+1;
if(n==1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next = NULL;
return head;
}
struct student * create(struct student *head,int num)
{
struct student *p1,*p2,*p3,*hcr;
p1=p2=head;
int s=0;
hcr = NULL;
while(p1 != NULL)
{
s = s+1;
if(s==1)
{
hcr = p1;
}
else
if(s!=num)
{
p2->next = p1;
}
else
{
printf("请输入你要插入的数据: ");
p3 = (struct student *)malloc(LEN);
scanf("%ld,%f",&p3->num,&p3->score);
p3->next = p1;
p2 ->next = p3;
}
if(s!=num)
{
p2 = p1;
}
else
{
p3 = p1;
p2 = p3;
}
p1 = p1->next;
}
p2->next = NULL;
return hcr;
}
void main()
{
struct student *pt,*pcr,*t;
pt=creat();
t=pt;
do
{
printf("%ld,%f\n",pt->num,pt->score);
pt = pt->next;
}while(pt != NULL);
printf("\n");
printf("请输入你要插入的链位置: ");
int num;
scanf("%d",&num);
pcr = create(t,num);
do
{
printf("%ld,%f\n",pcr->num,pcr->score);
pcr = pcr->next;
}while(pcr != NULL);
}
typedef
typedef指向函数的指针
共用体
#include <stdio.h>
#include <string.h>
struct
{
int num;
char name[10];
char sex[10];
char job;
union
{
int banji;
char position[10];
}category;
}person[2];
void main()
{
int i;
for(i=0;i<2;i++)
{
printf("please input num: ");
scanf("%d",&person[i].num);
printf("please input name: ");
scanf("%s",&person[i].name);
printf("you sex is M|G: ");
scanf("%s",&person[i].sex);
printf("you job is s|t: ");
scanf("%s",&person[i].job);
if(person[i].job=='s')
{
printf("please input baiji num: ");
scanf("%d",&person[i].category.banji);
}
else
{
printf("please input position: ");
scanf("%s",&person[i].category.position);
}
printf("\n\n");
}
for(i=0;i<2;i++)
{
printf("\tnum\tname\tsex\tjob\tbanji/position\n");
if(person[i].job=='s')
{
printf("\t%d\t%s\t%s\t%c\t%d\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.banji);
}
else
{
printf("\t%d\t%s\t%s\t%c\t%s\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.position);
}
}
}
枚举类型
#include <stdio.h>
void main()
{
enum boy{
Tom,Danny,Gan,LiLei};
enum boy month[31],j;
int i;
j = Tom;
for(i=1;i<=30;i++)
{
month[i] = j;
j=boy(j+1);
if(j > LiLei)
{
j = Tom;
}
}
for(i=1;i<=30;i++)
{
switch(month[i])
{
case Tom: printf(" %4d %s\t",i,"Tom");
break;
case Danny: printf(" %4d %s\t",i,"Danny");
break;
case Gan: printf(" %4d %s\t",i,"Gan");
break;
case LiLei: printf(" %4d %s\t",i,"LiLei");
break;
default:
break;
}
if(i%5==0)
{
printf("\n");
}
}
printf("\n");
}