Chapter2 8
Chapter2 8
Contents
3
Structures (struct)
4
Declaring Structures (struct)
5
8.2. Copy a struct with typedef
7
Typedef & Struct
8
8.3.Accessing struct members (Fields)
9
Sample Program With Structs
10
Sample Program With Structs
int main ( )
{
struct student js = {"20239048","Joe Smith"}, *ptr = &js ;
js.birthdate.day = 7;
js.birthdate.month = 3;
js.birthdate.year = 2003;
js.gpa = 3.67;
printf ("%s %s %d/%d/%d %1.2f\n", js.studentid, js.name,
js.birthdate.day, js.birthdate.month, js.birthdate.year,
js.gpa) ;
printf ("%s %s %d/%d/%d %1.2f\n", ptr->studentid, ptr->name,
ptr->birthdate.day, ptr->birthdate.month, ptr->birthdate.year,
ptr->gpa) ;;
}
11
8.4. Array of structs
13
struct array example
int main ( )
{
int n,i, M, Y;
struct baby B[10];
do
{printf ("Input number of babies:");
scanf("%d",&n);}
while( n<1 ||n>10);//check n between 1 and 10
for(i=0;i<n;i++) //Input babies’ information
15
struct array example
fflush(stdin);
printf("\nName");
gets(B[i].Name);
scanf("%d",&B[i].Sex);
printf("Weight");
scanf("%f",&B[i].Weight);
} 16
struct array example