国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

4.2 結構類型——結構

2019-11-08 19:26:51
字體:
來源:轉載
供稿:網友

1. 結構

#include<stdio.h>int main(int argc, const char *argv){ struct date{ int month; int day; int year; }; struct date today; today.month = 02; today.day = 16; today.year = 2017; // Today is 2017-2-16 2.聲明在函數內外

和本地變量一樣:

在函數內部聲明的結構類型,只能在函數內部使用在函數外部聲明的結構類型,可以被多個函數使用,通常在函數外部

3.聲明結構的形式

// 方式一 struct point { int x; int y; }; struct point p1, p2;// p1 和 p2 都是point,都有x y // 方式二 struct { int x; int y; } p1, p2; //沒有聲明 point, p1, p2 是一種無名結構,有 x y // 方式三 struct point{ int x; int y; } p1, p2; // p1 p2 都是point,里邊有 x y

4.結構的初始化

#include<stdio.h>struct date { int month; int day; int year;};int main(int argc, const char *argv){ struct date today = { 02, 16, 2017 }; printf("Today is %i-%i-%i/n", today.year, today.month, today.day); // Today is 2017-2-16 // .c 文件支持,.cpp 不支持會報錯:sorry, unimplemented: non-trivial designated initializers not supported struct date Month = {.year = 2017, .month = 02}; printf("this month is %i-%i-%i/n", Month.year, Month.month, Month.day); // this month is 2017-2-0 return 0;}

5. 結構成員

變量名.成員名

#include<stdio.h>struct date { int month; int day; int year;};int main(int argc, const char *argv){ struct date today = { 02, 16, 2017 }; printf("Today is %i-%i-%i/n", today.year, today.month, today.day); // Today is 2017-2-16 return 0;}

6.結構運算

用結構變量的名字 訪問整個結構對于整個結構,可以做 賦值、取地址、函數參數 p1 = (struct point){5, 10}; // 相當于 p1.x = 5; p1.y = 10;p1 = p2; // 相當于 p1.x = p2.x; p1.y = p2.y;#include<stdio.h>struct date { int month; int day; int year;};int main(int argc, const char *argv){ struct date today; today = (struct date){ 02, 16, 2017 }; struct date day; day = today; day.year = 2015; printf("day is %i-%i-%i/n", day.year, day.month, day.day); // day is 2015-2-16 return 0;}

7. 結構指針

用 & 取結構體的地址

#include<stdio.h>struct date{ int month; int day; int year;};int main(int argc, const char *argv){ struct date today; today = (struct date){ 02, 16, 2017}; struct date *pDate = &today; printf("address of today is %p/n", today); // address of today is 000000000022FE20 printf("address of pDate is %p/n", pDate); // address of pDate is 000000000022FE30 printf("pDate=%X/n", *pDate); // pDate=22FE20 return 0;}

8. 結構作為函數參數

int numberOfDay(struct date d)

整個結構可以作為函數參數,在函數內新建一個結構變量,并復制調用者的結構的值函數可以返回一個結構

輸入今天的日期,打印明天的日期

#include<stdio.h>#include<stdbool.h>struct date{ int month; int day; int year;};bool isLeap(struct date d);int numberOfDays(struct date d);int main(int argc, const char *argv){ struct date today, tomorrow; printf("輸入今天的日期(mm dd yyyy):"); // 輸入今天的日期(mm dd yyyy):02 16 2017 scanf("%i %i %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; } // Tomorrow's date is 2017-2-17 printf("Tomorrow's date is %i-%i-%i/n", tomorrow.year, tomorrow.month, tomorrow.day); return 0;}int numberOfDays(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; } 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;}

9.輸入結構

沒有直接的方式可以一次 scanf 一個結構,比如我們有一個函數 用于讀入一個坐標的x y值,該函數中可以操作這個結果,但是怎么傳出去呢? 可以在函數中,創建一個臨時的結構變量,然后 返回這個結構變量

#include<stdio.h>struct point { int x; int y;};struct point getPoint(void);void printPoint(struct point);int main(int argc, const char *argv){ struct point y = {0,0}; y = getPoint(); printPoint(y); return 0;}struct point getPoint(){ struct point p; scanf("%d", &p.x);// 輸入 2 回車 3 回車 scanf("%d", &p.y); printf("getPoint x=%d, y=%d/n", p.x, p.y); // getPoint x=2, y=3 return p;}void printPoint(struct point p){ printf("printfPoint x=%d, y=%d/n", p.x, p.y); // printfPoint x=2, y=3}

10.指向結構的指針 作為參數

當傳遞給函數 一個很大的結構,傳指針 比 結構 更高效

用 -> 表示指針所指的結構變量中的成員

#include<stdio.h>struct point { int x; int y;};struct point* getPoint(struct point*);void printPoint(const struct point*);int main(int argc, const char *argv){ struct point y = {0,0}; getPoint(&y); printPoint(&y);// printPoint(getPoint(&y)); return 0;}struct point* getPoint(struct point* p){ scanf("%d", &p->x);// 輸入 2 回車 3 回車 scanf("%d", &p->y); printf("getPoint x=%d, y=%d/n", p->x, p->y); // getPoint x=2, y=3 return p;}void printPoint(const struct point *p){ printf("printfPoint p->x=%d, p->y=%d/n", p->x, p->y); // printfPoint p->x=2, p->y=3}

11.結構數組

#include<stdio.h>struct time{ int hour; int minute; int second;};struct time timeUpdate(struct time now);int main(int argc, const char *argv){ struct time times[] = { {11, 59, 59}, {12, 1, 0}, {1, 29, 59}, {23, 59, 59}, {19, 12, 27} }; int i; for(i = 0; i < 5; i++){ printf("Time is %.2i:%.2i:%.2i/n", times[i].hour, times[i].minute, times[i].second); times[i] = timeUpdate(times[i]); printf("1s laster, Time is %.2i:%.2i:%.2i/n", times[i].hour, times[i].minute, times[i].second); } return 0;}// 計算1s之后的時間并返回 struct time timeUpdate(struct time now){ ++now.second; if(now.second == 60){ now.second = 0; ++now.minute; if(now.minute == 60){ now.minute = 0; ++now.hour; if(now.hour == 24) { now.hour = 0; } } } return now;}

12.嵌套的結構

struct point{ int x; int y;};struct rectangle{ struct point p1; struct point p2;};// 如果有 struct rectangle r; 就可以有 r.p1.x r.p2.y// 如果有: struct rectangle r, *rp;rp = &r;// 下邊四種形式是等價的 r.p1.x;rp->p1.x;(r.p1).x;(rp->p1).x;

13.結構中的結構數組

#include<stdio.h>struct point{ int x; int y;};struct rectangle{ struct point p1; struct point p2;};void printRec(struct rectangle r){ printf("<%d,%d> to <%d,%d>/n", r.p1.x, r.p1.y, r.p2.x, r.p2.y);}int main(int argc, const char *argv){ struct rectangle rects[] = { {{1, 2}, {3, 4}}, {{5, 6}, {7, 8}} }; int i; for(i = 0; i < 2; i++){ printRec(rects[i]); } return 0; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙海市| 永登县| 永寿县| 郧西县| 孙吴县| 双鸭山市| 岑巩县| 交城县| 曲阜市| 沙湾县| 水富县| 贵州省| 哈巴河县| 濮阳市| 沛县| 阿拉善盟| 乌拉特中旗| 大城县| 岗巴县| 上蔡县| 咸宁市| 娱乐| 西乌珠穆沁旗| 徐汇区| 分宜县| 修武县| 获嘉县| 新和县| 时尚| 华池县| 琼结县| 龙里县| 习水县| 常熟市| 宁南县| 香港 | 屏东县| 徐水县| 蒲江县| 同仁县| 阳新县|