和本地变量一样:
在函数内部声明的结构类型,只能在函数内部使用在函数外部声明的结构类型,可以被多个函数使用,通常在函数外部变量名.成员名
#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;}用 & 取结构体的地址
#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;}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;}没有直接的方式可以一次 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}当传递给函数 一个很大的结构,传指针 比 结构 更高效
用 -> 表示指针所指的结构变量中的成员
#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}新闻热点
疑难解答