1. 其实*p和**pm都是差不多的,一个是告诉系统是一级指针和二级指针,判断他们是否是二级指针还是看他们保存的是否是指针变量的地址
2.写的程序哪段跳过就是哪段有问题
#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);//malloc返回值是void指针,要改成选民的指针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;int j;int mark;char tmpname[32];int feipiao = 0;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;int i;max = p;for(i = 0;i<len;i++){if(max->tickets < p->tickets){max = p;}p++;}return max;
}int main(){struct xuanMin *xm = NULL; //*xm是指向一个空地址struct xuanMin *final;int total = 0;struct xuanMin **pxm = &xm; //**pm保 存的是*xm的地址int *pt = &total; //int i;//*pt = 0;//*pm = NULL;if(*pxm == NULL){printf("请输入有几人参选:\n");scanf("%d",pt);*pxm = (struct xuanMin*)malloc(*pt * sizeof(struct xuanMin));}for(i = 0;i<*pt;i++){//pxm 是xm的地址, xm是指针变量,存放的是别人的地址,所以*pxm取地址,(*pxm)->tickets == 0;//(*pxm)也是地址,当用地址的方式来访问结构体里面的内容要用->.printf("请输入第%d个选民的名字:\n",i+1);scanf("%s",(*pxm)->name);(*pxm)++; //偏移}*pxm = *pxm - *pt;//initXms(xm,&total);printXms(xm,total);int feip = doVot(xm,total);printf("废票数是: %d\n",feip);printXms(xm,total);final = getMax(xm,total);printf("%s以%d票当选!!废票是%d\n",final->name,final->tickets,feip);return 0;
}