有趣的结构体问题,希望各位发表意见
看代码程序代码:
#include "stdio.h" #include "stdlib.h" struct STUDENT { char name[15]; char number[15]; int score[5]; }; //输入学生的5门成绩,并输出,学生个数由你输入 int main(int argc, char* argv[]) { printf("Hello World!\n"); int n, i, j; struct STUDENT *p; printf("Please enter n:"); scanf("%d", &n); p=(struct STUDENT *)malloc(sizeof(struct STUDENT)*n); if(p==NULL) { printf("No enough memory\n"); exit(0); } for(i=0; i < n; i++) { printf("Please enter the student's name:"); scanf("%s", &(p+i)->name);//问题:为什么用&(p+i)->name和(p+i)->name都正确可以运行? //scanf("%s", (p+i)->name); printf("Please enter the student's number:"); scanf("%s", (p+i)->number);//这里不加&;为什么它能运行? for(j=0; j < 5; j++) { printf("Please enter the student's score %d:", j+1); scanf("%d", &(p+i)->score[j]);//为什么这里要加&?如果用(p+i)->score[j]程序会错误; } } for(i=0; i < n; i++) { printf("name:%s\n", (p+i)->name);//问题:为什么这不能加*号?加*号会错误,下面也一样! printf("number:%s\n", (p+i)->number); printf("maths\tEnglish\tChinese\tphysicial\tchemical\n"); for(j=0; j < 5; j++) printf("%d\t", (p+i)->score[j]); printf("\n"); } free(p); return 0; }