1. 结构体指针变量引入(103.8.6)
2. 通过结构体指针来访问结构体内容(104.8.7)
#include <stdio.h>
struct Test
{
int idata;
char cdata;
};
int main()
{
int a = 10;
int *p = &a;
char c = 'c';
char *pc = &c;
struct Test t1 = {10,'c'};
struct Test *pt = &t1;
printf("t1的idata=%d\n",t1.idata);
printf("t1的idata=%d\n",pt->idata);
pt->cdata = 'R';
printf("t1的cdata=%c\n",t1.cdata);
printf("t1的cdata=%c\n",pt->cdata);
return 0;
}
3. 结构体指针应用01(105.8.8)
#include <stdio.h>
struct Student
{
int num;
char name[32];
char sex[32];
int age;
double score;
char addr[32];
};
int main()
{
int i,len;
struct Student arr[3]={
{1,"Jessie","男",24,99.59,"郑州"},
{2,"Oxygen","男",20,99.5,"洛阳"},
{3,"Zhouzhou","男",21,97.6,"吉林"}
};
struct Student *p;
p=arr;
len=sizeof(arr)/sizeof(arr[0]);
for(i=0;i<len;i++){
printf("学号:%d,姓名:%s,性别:%s,年龄:%d,分数:%lf,地址:%s\n\n",
p->num,p->name,p->sex,p->age,p->score,p->addr);
p++;
}
p=arr;
for(i=0;i<len;i++){
printf("学号:%d,姓名:%s,性别:%s,年龄:%d,分数:%lf,地址:%s\n\n",
p->num,p->name,p->sex,p->age,p->score,p->addr);
p++;
}
return 0;
}
4. 结构体指针应用02之改写选票系统(106.8.9)
#include <stdio.h>
#include <string.h>
struct XuanMin
{
char name[32];
int tickets;
};
int main()
{
struct XuanMin xm[3];
struct XuanMin max;
struct XuanMin *p=xm;
int i,j;
int len = sizeof(xm)/sizeof(xm[0]);
char tmpName[32];
int mark = 0;
int feiPiao = 0;
for(i = 0; i<len; i++){
p->tickets = 0;
printf("请输入第%d个选民的名字:\n",i+1);
scanf("%s",p->name);
p++;
}
for(i=0;i<5;i++){
mark = 0;
printf("请输入你要投给谁:\n");
memset(tmpName,'\0',sizeof(tmpName));
scanf("%s",tmpName);
p=xm;
for(j = 0;j<len; j++){
if(strcmp(tmpName,p->name) == 0){
p->tickets++;
mark = 1;
}
p++;
}
if(mark == 0){
printf("没有此候选人,放弃\n");
feiPiao++;
}
}
p=xm;
for(i=0;i<len;i++){
printf("名字:%s,票数:%d\n",p->name,p->tickets);
p++;
}
max = xm[0];
p=xm;
for(i=0;i<len;i++){
if(max.tickets < p->tickets){
max = xm[i];
}
p++;
}
printf("%s以%d票当选!废票有%d票。\n",max.name,max.tickets,feiPiao);
}
5. 结构体指针数组函数综合应用改写选票系统(107.8.10)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct XuanMin
{
char name[32];
int tickets;
};
struct XuanMin* initXms(struct XuanMin *p,int *pn)
{
int i;
if(p==NULL){
printf("请输入有几个人参选:\n");
scanf("%d",pn);
p = (struct XuanMin*)malloc(*pn*sizeof(struct XuanMin));
}
for(i=0;i<*pn;i++){
p->tickets = 0;
printf("请输入第%d个选民的名字:\n",i+1);
scanf("%s",p->name);
p++;
}
return p-*pn;
}
void printXms(struct XuanMin *p,int len)
{
int i;
for(i=0;i<len;i++){
printf("名字:%s,票数:%d\n",p->name,p->tickets);
p++;
}
}
int doVot(struct XuanMin *p,int len)
{
int i,j,mark;
int feiPiao=0;
char tmpName[32];
struct XuanMin *pbak=p;
for(i=0;i<5;i++){
mark = 0;
printf("请输入你投给谁:\n");
memset(tmpName,'\0',sizeof(tmpName));
scanf("%s",tmpName);
p = pbak;
for(j = 0;j<len; j++){
if(strcmp(tmpName,p->name) == 0){
p->tickets++;
mark = 1;
}
p++;
}
if(mark == 0){
printf("没有此候选人,放弃\n");
feiPiao++;
}
}
return feiPiao;
}
struct XuanMin* getMax(struct XuanMin *p,int len)
{
struct XuanMin* max=p;
for(int i=0;i<len;i++){
if(max->tickets < p->tickets){
max = p;
}
p++;
}
return max;
}
int main()
{
struct XuanMin *xm=NULL;
int total=0;
xm=initXms(xm,&total);
printXms(xm,total);
int feip=doVot(xm,total);
printf("废票数是:%d\n",feip);
printXms(xm,total);
struct XuanMin *final;
final=getMax(xm,total);
printf("%s以%d票当选!废票是%d票。\n",final->name,final->tickets,feip);
return 0;
}
6. 结构体二级指针(108.8.11)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct XuanMin
{
char name[32];
int tickets;
};
void initXms(struct XuanMin **pxm,int *pt)
{
int i;
if(*pxm == NULL){
printf("请输入有几个人参选:\n");
scanf("%d",pt);
printf("一共%d个人参选\n",*pt);
*pxm = (struct XuanMin*)malloc(*pt * sizeof(struct XuanMin));
}
for(i=0;i<*pt;i++){
(*pxm)->tickets = 0;
printf("请输入第%d个选民的名字:\n",i+1);
scanf("%s",(*pxm)->name);
(*pxm)++;
}
*pxm = *pxm - *pt;
}
void printXms(struct XuanMin *p,int len)
{
int i;
for(i=0;i<len;i++){
printf("名字:%s,票数:%d\n",p->name,p->tickets);
p++;
}
}
int doVot(struct XuanMin *p, int len)
{
int i,j,mark;
int feiPiao=0;
char tmpName[32];
struct XuanMin *pbak=p;
for(i=0;i<5;i++){
mark = 0;
printf("请输入你投给谁:\n");
memset(tmpName,'\0',sizeof(tmpName));
scanf("%s",tmpName);
p = pbak;
for(j = 0;j<len; j++){
if(strcmp(tmpName,p->name) == 0){
p->tickets++;
mark = 1;
}
p++;
}
if(mark == 0){
printf("没有此候选人,放弃\n");
feiPiao++;
}
}
return feiPiao;
}
struct XuanMin* getMax(struct XuanMin *p,int len)
{
struct XuanMin* max=p;
for(int i=0;i<len;i++){
if(max->tickets < p->tickets){
max = p;
}
p++;
}
return max;
}
int main()
{
struct XuanMin *xm = NULL;
int total = 0;
initXms(&xm,&total);
printXms(xm,total);
int feip = doVot(xm,total);
printf("废票数是:%d\n",feip);
printXms(xm,total);
struct XuanMin *final;
final = getMax(xm,total);
printf("%s以%d票当选!!废票是%d\n",final->name,final->tickets,feip);
return 0;
}