Focus: Pointer self-add, point to next struct array cell
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 #defineMax 105 #defineMin 36 7typedefstructstu{8 Char*name;9 intAge ;Ten intscore; One } Stu; A - //input - voidInput (STU *); the voidOutput (STU *); - voidClean (STU *); - - voidMainvoid) + { - Stu Student[min]; +Input (student);//equivalent to incoming &student[0] A at output (student); - Clean (student); - } - - voidInput (STU *student) - { in Chartemp[ -]; - intI=0; toPuts"Please input name"); + while(I<min & Gets (temp)! = NULL & temp[0] !=' /' ) - { theStudent->name = (Char*) malloc (strlen (temp) +1); *strcpy (student->name,temp); $ Panax NotoginsengPuts"Age ?"); -scanf"%d",&student->Age ); thePuts"score?"); +scanf"%d",&student->score); A thei++; +student++;//pointer self-add, point to next array cell - $ while(GetChar ()! ='\ n') $ Continue; - - if(i<min) thePuts"Next name"); - }Wuyi the } - Wu voidOutput (STU *student) - { About intI=0; $ while(i<min) { -printf"%s---%d---%d\n",student->name,student->age,student->score); -i++; -student++; A } + the } - $ voidClean (STU *student) the { the intI=0; the while(i<min) { theFree (student->name); -i++; instudent++; the } the}
C manipulating struct arrays with pointers