C Development Learning-structure type (enumeration/structure/type definition)

Source: Internet
Author: User

C Development Learning-structure type (enumeration/structure/type definition)
I. Enumeration definition: enumeration is a user-defined data type. The enum Enumeration type name used by the keyword is generally not used. It uses a name in braces, because they are constants, their types are int, and their values are from 0 to n. Enum colors {red, yellow, green} Syntax: enum Enumeration type name {name 0 m,..., name n };
Case 1: enumeration of automatic count

/// Main. c // enum /// Created by liuxinming on 15/4/26. // Copyright (c) 2015 liuxinming. All rights reserved. // # include
 
  
Enum COLOR {RED, YELLOW, GREEN, NumCOLORS}; int main (int argc, const char * argv []) {int color =-1; char * ColorNames [NumCOLORS] = {red, yellow, green}; char * colorName = NULL; printf (enter your preferred color code :); scanf (% d, & color ); if (color> = 0 & color <NumCOLORS) {colorName = ColorNames [color];} else {colorName = unknown;} printf (your preferred color is % s, colorName ); return 0 ;}
 

Case 2: You can specify the enum COLOR {RED = 1, YELLOW, GREEN = 5} when declaring the enumeration}
2. The structure is a combination of various variables consisting of basic data types and named by an identifier.
Different data types can be used in the structure.
Declarative Structure
Struct point {int x; int y;} struct point p1, p2; # Both p1 and p2 are point, which contains the values of x and y.

Struct {int x; int y;} p1, p2; # p1 and p2 are all untitled structures with values of x and y.

Structure Variable
Struct point p; # p is a structure variable p. x = 12; p. y = 20;

Case 1: use struct
/// Main. c // structure /// Created by liuxinming on 15/4/12. // Copyright (c) 2015 liuxinming. All rights reserved. // # include
 
  
// Declare the structure type struct date {int month; int day; int year ;}; int main (int argc, const char * argv []) {// Structure Variable & use struct date today; today. month = 04; today. day = 12; today. year = 2015; printf (Today is date is % I-% I ., today. year, today. month, today. day); return 0 ;}
 


Case 2: Structure Initialization
/// Main. c // structure /// Created by liuxinming on 15/4/12. // Copyright (c) 2015 liuxinming. All rights reserved. // # include
 
  
// Declare the structure type struct date {int month; int day; int year ;}; int main (int argc, const char * argv []) {struct date today = {04, 26,201 5}; struct date thismonty = {. month = 4 ,. year = 2015}; printf (Today is date is % I-% I ., today. year, today. month, today. day); printf (This month is % I-% I ., thismonty. year, thismonty. month, thismonty. day); return 0 ;}
 

Output:
Today is date is 2015-4-26.This month is 2015-4-0.

Structure member * the structure and array are a bit like [There are many units in the array, and many Members in the Structure * the array uses the [] Operator and subscript to access its member a [0] = 10; * structure. operator and name to access its member today. day structure operation * to access the entire structure, directly use the name of the Structure Variable * for the entire structure, you can assign a value, get the address, or pass it to the function parameter p1 = (struct point) {5, 10} // equivalent to p1.x = 5 p1.y = 10; p1 = p2; // equivalent to p1.x = p2.x; p1.y = p2.y;
The structure pointer * is different from the array. The structure variable name is not the address of the Structure Variable. You must use the & operator * struct date * pDate = & today;
/// Main. c // structure /// Created by liuxinming on 15/4/12. // Copyright (c) 2015 liuxinming. All rights reserved. // # include
 
  
// Declare the structure type struct date {int month; int day; int year ;}; int main (int argc, const char * argv []) {struct date today; today = (struct date) {04, 26,201 5}; struct date day; struct date * pDate = & today; printf (Today's date is % I-% I ., today. year, today. month, today. day); printf (The day's date is % I-% I ., day. year, day. month, day. day); printf (address of today is % p, pDate); return 0 ;}
 
Output:
Today's date is 2015-4-26.The day's date is 0-1606416456-32767.address of today is 0x7fff5fbff7f0Program ended with exit code: 0

The structure is used as the function parameter int numberOfDays (struct date d) * the entire structure can be passed into the function as the parameter value * at this time, a new structure variable is created in the function, copy the value of the caller's structure. * You can also return a Structure CASE: Enter today's date and output tomorrow's date. [This section describes the structure usage and does not describe the implementation process, I want to know about it myself]
/// Main. c // structure /// Created by liuxinming on 15/4/12. // Copyright (c) 2015 liuxinming. All rights reserved. // # include
 
  
# Include
  
   
// Declare the structure type struct date {int month; int day; int year ;}; bool isLeap (struct date d ); // judge whether it is a leap year int numberOfDays (struct date d); int main (int argc, const char * argv []) {struct date today, tomorrow; printf (Enter today's date (mm dd yyyy) :); scanf (% I, & today. month, & today. day, & today. year); if (today. day! = NumberOfDays (today) {tomorrow. day = today. day + 1; tomorrow. month = today. month; tomorrow. year = today. year;} else if (today. month = 12) {tomorrow. day = 1; tomorrow. month = 1; tomorrow. year = today. year + 1;} else {tomorrow. day = 1; tomorrow. month = today. month + 1; tomorrow. year = today. year;} printf (Tomorrow's date is % I-% I ., tomorrow. year, tomorrow. month, tomorrow. day); return 0;} int n UmberOfDays (struct date d) {int days; const int daysPerMonth [12] = {31,28, 31,30, 31,30, 31,31, 30,31, 30,31}; if (d. month = 2 & isLeap (d) {days = 29; // leap year} else {days = daysPerMonth [d. month-1];} return days;} bool isLeap (struct date d) {bool leap = false; if (d. year % 4 = 0 & d. year % 100! = 0) | d. year % 400 = 0) {leap = true;} return leap ;}
  
 

Output:
Enter today's date(mm dd yyyy):12 31 2014Tomorrow's date is 2015-1-1.Program ended with exit code: 0

Pointer to structure
struct date {  int month;  int day;  int year;} myday;struct date *p = &myday;(*p).month = 12;p->month = 12;

* Use-> to indicate the member in the structure variable to which the Pointer Points
/// Main. c // structure /// Created by liuxinming on 15/4/12. // Copyright (c) 2015 liuxinming. All rights reserved. // # include
 
  
Struct point {int x; int y ;}; struct point * getStruct (struct point *); void output (struct point); void print (const struct point * p ); int main (int argc, const char * argv []) {struct point y = {0, 0}; getStruct (& y); output (y ); output (* getStruct (& y); print (getStruct (& y); return 0;} struct point * getStruct (struct point * p) {scanf (% d, & p-> x); scanf (% d, & p-> y); printf (% d, % d, p-> x, p-> y ); return p;} void output (struct point p) {printf (% d, % d, p. x, p. y);} void print (const struct point * p) {printf (% d, % d, p-> x, p-> y );}
 

Output:
105010, 5010, 50

Structure Array
struct date dates[100];struct date dates[] = { {4,5,2005}, {2,4,2005}};

Case:
/// Main. c // structure /// Created by liuxinming on 15/4/12. // Copyright (c) 2015 liuxinming. All rights reserved. // # include
 
  
Struct time {int hour; int minutes; int seconds ;}; struct time timeUpdate (struct time now); int main (void) {struct time testTime [5] ={{ 11, 59, 59 },{, 0 },{, 29 },{, 59 },{, 27 }}; int I; for (I = 0; I <5; I ++) {printf (Time is %. 2i: %. 2i: %. 2i, testTime [I]. hour, testTime [I]. minutes, testTime [I]. seconds); testTime [I] = timeUpdate (testTime [I]); printf (... one second later it's %. 2i: %. 2i: %. 2i, testTime [I]. hour, testTime [I]. minutes, testTime [I]. seconds);} return 0;} struct time timeUpdate (struct time now) {++ now. seconds; if (now. seconds = 60) {now. seconds = 0; ++ now. minutes; if (now. minutes = 60) {now. minutes = 0; ++ now. hour; if (now. hour = 24) {now. hour = 0 ;}} return now ;}
 

Output:
Time is 11:59:59... one second later it's 12:00:00Time is 12:00:00... one second later it's 12:00:01Time is 01:29:29... one second later it's 01:29:30Time is 23:59:59... one second later it's 00:00:00Time is 19:12:27... one second later it's 19:12:28Program ended with exit code: 0

Structure
struct dateAndTime{  struct date sdate;  struct time stime;}

Nested Structure
Struct rectangle {struct point pt1; struct point pt2 ;}; // if there is a variable struct rectangle r; // You Can Have: r. pt1.x = 1; r. pt1.y = 2; r. pt2.x = 11; r. pt2.y = 22; // If a variable defines struct rectangle * rp; rp = & r; //, the four forms are equivalent. // r. pt1.x, rp-> pt1.x, (r. pt1 ). x, (rp-> pt1 ). x // but no rp-> pt1-> x because pt1 is not a pointer




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.