The example introduces the usage of the C language structure.

Source: Internet
Author: User

*/
# 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.
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.