问题描述
- (c语言)利用指向结构的指针先输入数据,后打印数据,为什么会出现打印出来的都是0?
-
#include#define MAXFIRST 20
#define MAXLAST 20
#define G_STORE 3
#define A_STRORE 3
#define CSIZE 4
struct name {
char first[MAXFIRST];
char last[MAXLAST];};
struct student{
struct name s_name;
float grade[G_STORE];
float average;};
void e_function(struct student * ps[],int count);
void f_function(struct student * ps[],int count);
void g_function(struct student * ps[],int count);int main(void)
{
int index;
int count=0;
struct student solve[CSIZE];
struct student * psolve[CSIZE];
for(index=0;index<CSIZE;index++)
psolve[index]=&solve[index];printf("Please enter your first name:n");
while(counts_name.first)!=NULL&&psolve[count]->s_name.first[0]!='')
{
printf("Please enter your last name:n");
gets(psolve[count]->s_name.last);
printf("Please enter your Chinese grade,Math grade and English grade:n");
scanf("%.2f %.2f %.2f",&psolve[count]->grade[0],&psolve[count]->grade[1],&psolve[count]->grade[2]);
count++;
while(getchar()!='n')
continue;
printf("Enter next student's first name:n");
}e_function(psolve,count); f_function(psolve,count); g_function(psolve,count); return 0;
}
void e_function(struct student * ps[],int count)
{int index; for(index=0;index<count;index++) ps[index]->average=(ps[index]->grade[0]+ps[index]->grade[1]+ps[index]->grade[2])/3;
}
void f_function(struct student * ps[],int count)
{
int index;
for(index=0;index
{
printf("%s %s ",ps[index]->s_name.first,ps[index]->s_name.last);
printf("Chinese grade,Math grade and English grade:%.2f %.2f %.2fn",ps[index]->grade[0],ps[index]->grade[1],ps[index]->grade[2]);}
}
void g_function(struct student * ps[],int count)
{
int index;
for(index=0;index
printf("average grade:%.2fn",ps[index]->average);
}
解决方案
太长了,没指到正确的变量。或者该变量没赋值
解决方案二:
while(counts_name.first!=NULL&&psolve[count]->s_name.first[0]!='')
这句话你拷错了吧?counts_name没有定义。
解决方案三:
psolve[count]数字在使用前没有初始化。
解决方案四:
最好的办法就是单步调试,比较容易分析原因