*/
# Include <stdio. h>
Struct date {// define a date Structure
Int year; // year
Int month; // month
Int day; // day
};
Struct student {// define a student structure, where birthday is a date structure, that is, the structure can be nested
Char * name; // name
Int id; // id card number
Struct date birthday; // birthday
};
Void quoteStructNormal () {// normal call of the Test Structure
Struct student liky;
Liky. name = "liky green ";
Liky. id = 7173;
Liky. birthday. year = 1990;
Liky. birthday. month = 10;
Liky. birthday. day = 18;
Printf ("likys name: % s", liky. name );
Printf ("likys id: % d", liky. id );
Printf ("likys birthday year: % d", liky. birthday. year );
Printf ("likys birthday month: % d", liky. birthday. month );
Printf ("likys birthday day: % d", liky. birthday. day );
}
Void quoteStructPointer () {// call the test structure pointer
Struct student lucy = {"liky", 5173, {1990, 10, 18 }};
Struct student * liky;
Liky = & lucy;
(* Liky). id = 5173;
(* Liky). name = "liky ";
(* Liky). birthday. year = 1990;
(* Liky). birthday. month = 10;
(* Liky). birthday. day = 18;
Printf ("likys name: % s", (* liky). name );
Printf ("likys id: % d", (* liky). id );
Printf ("likys birthday year: % d", liky-> birthday. year );
Printf ("likys birthday month: % d", liky-> birthday. month );
Printf ("likys birthday day: % d", liky-> birthday. day );
}
// Test the structure pointer here
Void testDatePointer (){
Struct date * today;
Struct date tomorrow;
// = {1990,10, 18}
Today = & tomorrow;
Today-& gt; year = 1990;
Today-> month = 10;
Today-> day = 18;
Printf ("year: % d", (* today). year );
Printf ("month: % d", (* today). month );
Printf ("day: % d", (* today). day );
}
// The structure pointer is passed as a parameter.
Void testStructParam (struct student * stu ){
Printf ("stus name: % s", (* stu). name );
Printf ("stus id: % d", (* stu). id );
Printf ("stus birthday year: % d", stu-> birthday. year );
Printf ("stus birthday month: % d", stu-> birthday. month );
Printf ("stus birthday day: % d", stu-> birthday. day );
}
Void main (){
// QuoteStructNormal ();
// QuoteStructPointer ();
// TestDatePointer ();
// The following is the test structure array.
Struct student lucy = {"liky", 5173, {1990, 10, 18 }};
Struct student stus [3] = {"liky", 9173, {1990, 10, 18 },{ "lucky", 5173, {1990, 10, 18 }}, {"lucy", 6173, {1990, 10, 18 }}};
Int I;
For (I = 0; I <3; I ){
Printf ("stus [% d] name: % s", I, stus [I]. name );
Printf ("stus [% d] s id: % d", I, stus [I]. id );
Printf ("stus [% d] s birthday year: % d", I, stus [I]. birthday. year );
Printf ("stus [% d] s birthday month: % d", I, stus [I]. birthday. month );
Printf ("stus [% d] s birthday day: % d", I, stus [I]. birthday. day );
}
// The structure pointer is passed as a parameter.
TestStructParam (& lucy );
}
/*
Many people are not very familiar with the structure usage, especially the structure nesting and structure pointer. Here are some examples to help.
*/