码迷,mamicode.com
首页 > 其他好文 > 详细

结构体与共用体01

时间:2018-12-06 14:42:56      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:枚举类   core   不同类   优化   决定   定义   技术分享   inpu   col   

目录

  1. 概述
  2. 定义结构体类型变量的方法
  3. 结构体变量的引用
  4. 结构体变量的初始化
  5. 结构体数组
  6. 指向结构体类型数据的指针
  7. 用指针处理链表
  8. 共用体
  9. 枚举类型
  10. 用typedef定义类型

 

1.概述
问题定义:有时需要将不同类型的数据组合成一个有机的整体,以便于引用。

如:一个学生有学号/姓名/性别/年龄/地址等属性   int num; char name[20]; char sex; int age;   int char addr[30];技术分享图片

定义一个结构的一般形式为:
 struct 结构名
 {

成员表列

};

成员表列由若干个成员组成,每个成员都是该结构的一个组成部分。对每个成员也必须作类型说明,其形式为:类型说明符 成员名;

struct student
  {
      int num;
      char name[20];
      char sex;
      int age;
      float score;
      char addr[30];
  }

 

2.定义结构体类型变量的方法

可以采取以下3种方法定义结构体类型变量:

 

(1)先声明结构体类型再定义变量名
例如:struct  student  student1, student2;
                 |            |              |               |       
           类型名   结构体     变量名      变量名

定义了student1和student2为struct student类型的变量,即它们具有struct student类型的结构.

struct student
  {
      int num;
      char name[20];
      char sex;
      int age;
      float score;
      char addr[30];
  } student1, student2

 

在定义了结构体变量后,系统会为之分配内存单元。例如:  student1和student2在内存中各占 ? 个字节。( 4 + 20 + 1 + 4 + 4 + 30 =  67 )。不会占用67个字节,涉及到编译优化问题。

(2)在声明类型的同时定义变量
   这种形式的定义的一般形式为:
        struct 结构体名
     {
         成员表列
     }变量名表列;

 例如:

struct student
{           int num;
     char name[20];
     char sex;
     int age;
     float score;
     char addr[30];
}student1,student2;

 

 

(3) 直接定义结构体类型变量
   其一般形式为:
      struct
    {
      成员表列
  }变量名表列;

即不出现结构体名。

看图下定义:技术分享图片

结论:这是一个嵌套的定义

首先定义一个结构date,由month(月)、day(日)、year(年) 三个成员组成。

在定义并说明变量 boy1 和 boy2 时,其中的成员birthday被说明为date结构类型。成员名可与程序中其它变量同名,互不干扰。

struct date
{
    int month;
    int day;
    int year;
};

struct
{
    int num;
    char name[20];
    char sex;
    struct date birthday;
    float score;
}boy1, boy2;

 

3.结构体变量的引用
在定义了结构体变量以后,当然可以引用这个变量。但应遵守以下规则:   

(1) 不能将一个结构体变量作为一个整体进行输入和输出。
  例如: 打印student1的各个变量的值。不可以这样:printf(″%d,%s,%c,%d,%f,%\n″,student1);

正确引用结构体变量中成员的方式为:结构体变量名.成员名

student1.num表示student1变量中的num成员,即student1的num(学号)项。可以对变量的成员赋值,例如:student1.num=100;
“.”是成员(分量)运算符,它在所有的运算符中优先级最高,因此可以把student1.num作为一个整体来看待。上面赋值语句的作用是将整数100赋给student1变量中的成员num。
例子:

#include <stdio.h>

void main()
{
      struct student
      {
            int num;
            char *name;
            char sex;
            float score;
      } boy1, boy2;
      
      boy1.num = 007;
      boy1.name = "Jane";
      
      printf("Please input sex and score\n");
      scanf("%c %f", &boy1.sex, &boy1.score);
      
      boy2 = boy1;

      printf("Number = %d\nName = %s\n", boy2.num, boy2.name);
      printf("Sex = %c\nScore = %f\n", boy2.sex, boy2.score);
      
}

 

 

(2) 如果成员本身又属一个结构体类型,则要用若干个成员运算符,一级一级地找到最低的一级的成员。只能对最低级的成员进行赋值或存取以及运算。

对上面定义的结构体变量student1, 可以这样访问各成员:
    student1.num
    student1.birthday.month

/***********************************************************************************************/
/* 注意:                                                                                      */
/* 不能用student1.birthday来访问student1变量中的成员birthday,因为birthday本身是一个结构体变量。*/
/***********************************************************************************************/

#include <stdio.h>

void main()
{
      struct date            
      {          
            int month;            
            int day;            
            int year;            
      };
      
      struct
      {            
            int num;            
            char name[20];            
            char sex;            
            struct date birthday;            
            float score;            
      } boy1, boy2;
      
      
      printf("Please input birthday(YY:) ");
      scanf("%d", &boy1.birthday.year);
      printf("Please input birthday(MM:) ");
      scanf("%d", &boy1.birthday.month);
      printf("Please input birthday(DD:) ");
      scanf("%d", &boy1.birthday.day);
      printf("\n");

      boy2 = boy1;

      printf("boy1‘s birthday is %d-%d-%d\n", boy1.birthday.year, boy1.birthday.month, boy1.birthday.day);
      printf("boy2‘s birthday is %d-%d-%d\n", boy2.birthday.year, boy2.birthday.month, boy2.birthday.day);
}

 

(3) 对结构体变量的成员可以像普通变量一样进行各种运算(根据其类型决定可以进行的运算)。

例如:
   student2.score = student1.score;
   sum = student1.score + student2.score;
   student1.age++;  
   ++student2.age;

(4) 可以引用结构体变量成员的地址,也可以引用结构体变量的地址。

例子:

#include <stdio.h>

void main()
{
      struct student
      {
            int num;
            char *name;
            char sex;
            float score;
      } boy1;
      
      boy1.num = 007;
      boy1.name = "Jane";
      
      printf("The address of struct is %o :\n", &boy1 );
      printf("The address of num is %o :\n", &boy1.num );

}

结果:地址相同,结构体的地址同首个结构体变量地址,类似数组。


但不能用以下语句整体读入结构体变量:
  scanf(″%d,%s,%c,%d,%f,%s″,&student1);

结构体变量的地址主要用作函数参数,传递结构体变量的地址。



 

 







 




结构体与共用体01

标签:枚举类   core   不同类   优化   决定   定义   技术分享   inpu   col   

原文地址:https://www.cnblogs.com/tianqizhi/p/10075798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!