C语言学习笔记--结构体

结构体定义三种方式:

      1 #include<stdio.h>
      2 //第一种定义方法
      3 struct point {
      4         int x;
      5         int y;
      6 };
      7 struct point p1,p2;
      8 
      9 //第二种定义方法
     10 struct {
     11         int x;
     12         int y;
     13 } p1,p2;
     14 
     15 //第三种定义方法
     16 struct {
     17         int x;
     18         int y;
     19 } p1,p2;

这三种方法,对于第一和第三种,都声明了结构point。但是第二种没有,只声明了两个变量。

下面的小例子,说明结构类型的两种初始化方法。

      1 #include<stdio.h>
      2 struct date {
      3         int month;
      4         int day;
      5         int year;
      6 };
      7 int main()
      8 {
      9         struct date today = {2,18,2015};    //结构的初始化,方法1;同数组初始化。
     10         struct date thismonth = {.month = 3,.year = 2015};    //结构的初始化,方法2;指定对应的结构成员初始化。
     11         printf("Todays date is %i-%i-%i
",today.day,today.month,today.year)
     12         printf("This month date is %i-%i-%i
",thismonth.month = 2,thismonth.year = 2015}
     13         return 0;
     14 };

结构成员:

  结构和数组有点像,访问方法不同。数组是【】访问里面的元素;结构是用.来访问结构成员。

结构运算:

  要访问整个结构,可以直接访问结构变量的名字。

  对于整个结构可以赋值,取地址,可以传递给函数。

  p1 = struct point{5,10};  //相当于p1.x = 5,p1.y = 10

  p1  = p2;

结构指针:

  结构和数组不一样,数组是指针,可以直接取地址。对结构成员取地址,必须用&。

  struct date *pdate = &today;

结构作为参数函数:

  int numofdays(struct date d);

  整个结构可以作为参数的值传入函数。

  这个时候是在函数内部新建一个结构变量,并且赋值调用者结构的值。

  也可以返回一个结构。和数组不同。  

      1 #include<stdio.h>
      2 #include<stdbool.h>
      3 
      4 struct date {
      5         int month;
      6         int day;
      7         int year;
      8 };
      9 
     10 bool isLeap(struct date d);
     11 int numberOfDays(struct date d);
     12 
     13 int main(int argc,char const *argv[])
     14 {
     15         struct date today,tomorrow;
     16 
     17         printf("Enter today's date (mm dd yyyy):
");
     18         scanf("%i %i %i",&today.month,&today.day,&today.year);
     19 
     20         if ( today.day != numberOfDays(today)) {
     21                 tomorrow.day = today.day + 1;
     22                 tomorrow.month = today.month;
     23                 tomorrow.year = today.year;
     24         } else if ( today.month == 12){
     25                 tomorrow.day = 1;
     26                 tomorrow.month = 1;
     27                 tomorrow.year = today.year + 1;
     28         } else {
     29                 tomorrow.day = 1;
     30                 tomorrow.month =today.month + 1;
     31                 tomorrow.year = today.year;
     32         }
     33         printf("tomorrow's date is %i-%i-%i
",tomorrow.day,tomorrow.month,tomorrow.year);
     34         return 0;
     35 }
     36 
     37 int numberOfDays(struct date d)
     38 {
     39         int days;
     40         const int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
     41         if ( d.month == 2 && isLeap(d))
     42                 days = 29;
     43         else
     44                 days = daysPerMonth[d.month - 1];
     45         return days;
     46 }
     47 
     48 bool isLeap(struct date d)
     49 {
     50         bool leap = false;
     51         if ( (d.year %4 == 0 && d.year%100 != 0) || d.year%400 == 0 )
     52                 leap = true;
     53         return leap;
     54 } 

输入结构:

  scanf函数只能读入一个变量。怎么读入一个结构呢?

  可以调用一个函数来实现。注意:C语言的函数调用是传值的。

  所以在函数中的结构变量p和main函数中的结构变量p是不同的。 

      1 #include<stdio.h>
      2 
      3 struct point {
      4         int x;
      5         int y;
      6 };
      7 
      8 void getStruct(struct point p);
      9 void putStruct(struct point p);
     10 
     11 int main(int grvc,const char *agrv[])
     12 {
     13         struct point y ={0,0};
     14 //      y = {0,0};                ---->结构体的初始化只能用上面的方法,这种写法无法编译
     15         getStruct(y);
     16         putStruct(y);
     17 
     18         return 0;
     19 }
     20 
     21 void getStruct(struct point p)
     22 {
     23 //      struct point p;         ---->此处重复定义结构体。因为结构体在函数头已经定义。
     24         scanf("%d",&p.x);
     25         scanf("%d",&p.y);
     26         printf("您输入的结构体是%d %d
",p.x,p.y);
     27 }
     28 void putStruct(struct point p)
     29 {
     30         printf("%d %d
",p.x,p.y);
     31 }
运行结果: 输入 3 4 输出 3 4 // 0 0

  在函数读入p之后没有任何值返回给main,所以主函数结构值不变。

解决方案:

  在函数内部创建一个临时的结构变量p,返回一个结构变量。

      1 #include<stdio.h>
      2 
      3 struct point {
      4         int x;
      5         int y;
      6 };
      7 
      8 struct point getStruct(void);
      9 void putStruct(struct point p);
     10 
     11 int main(int grvc,const char *agrv[])
     12 {
     13         struct point y ={0,0};
     14         y = getStruct();
     15         putStruct(y);
     16 
     17         return 0;
     18 }
     19 
     20 struct point getStruct(void)
     21 {
     22         struct point p = {0,0};
     23         scanf("%d",&p.x);
     24         scanf("%d",&p.y);
     25         printf("您输入的结构体是%d %d
",p.x,p.y);
     26 
     27         return p;
     28 }
     29 void putStruct(struct point p)
     30 {
     31         printf("%d %d
",p.x,p.y);
     32 }
运行结果: 输入3 4 输出 3 4// 3 4

指向结构的指针:  

      1 #include<stdio.h>
      2 struct date {
      3         int month;
      4         int day;
      5         int year;
      6 } myday;
      7 
      8 struct date *p = &myday
      9 
     10 (*p).month = 12;
     11 p->month = 12;

用->表示指针所指结构变量中成员。 

      1 #include<stdio.h>
      2 
      3 struct point {
      4         int x;
      5         int y;
      6 };
      7 
      8 struct point* getStruct(struct point *p);
      9 void putStruct(struct point p);
     10 
     11 int main(int grvc,const char *agrv[])
     12 {
     13         struct point y ={0,0};
     14         getStruct(&y);
     15         putStruct(y);
     16         putStruct(*getStruct(&y));
     17 
     18         return 0;
     19 }
     20 
     21 struct point* getStruct(struct point *p)
     22 {
     23         scanf("%d",&p->x);
     24         scanf("%d",&p->y);
     25         printf("您输入的结构体是%d %d
",p->x,p->y);
     26 
     27         return p;
     28 }
     29 void putStruct(struct point p)
     30 {
     31         printf("%d %d
",p.x,p.y);
     32 }

  

原文地址:https://www.cnblogs.com/netcn/p/4372970.html