结构体部分元素无法输出
程序代码:
#define _STDC_WANT_LIB_EXT1_1 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct Horse Horse; struct Horse { int age; int hight; char name [20]; char father[20]; char mother[20]; }; int main () { Horse *pHorse[50]; int count =0; char test ='\0'; char judge='\0'; for (count;count<sizeof(pHorse)/sizeof(pHorse[0]);count++) { printf_s("Do you want to enter the details of a%s horse (Y or N)?",count?"nother":""); scanf("%c",&test); judge=tolower(test); if(judge=='n') { break; } pHorse[count]=(Horse*)malloc(sizeof(Horse)); printf("请输入这匹马的名字:"); scanf_s("%s",pHorse[count]->name,sizeof(pHorse[count]->name)); putchar(10); fflush(stdin); printf("%s的年龄:",pHorse[count]->name); scanf_s("%d",&pHorse[count]->age); putchar(10); fflush(stdin); printf("%s的身高:",pHorse[count]->name); scanf_s("%d",&pHorse[count]->hight); putchar(10); fflush(stdin); printf("%s的父亲的名字:",pHorse[count]->name); scanf_s("%S",pHorse[count]->father); putchar(10); fflush(stdin); printf("%s的母亲的名字:",pHorse[count]->name); scanf_s("%S",pHorse[count]->mother); fflush(stdin); putchar(10); } for(int i=0;i<count;i++) { printf("%d\n",pHorse[i]->age); printf("%d\n",pHorse[i]->hight); printf("%s\n",pHorse[i]->father); printf("%s\n",pHorse[i]->mother); printf("%s\n",pHorse[i]->name); } system("pause"); return 0; }
编译器使用vc++2010;
下面为输出结果: