b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

使用C语言中的关键字struct(结构)

电脑杂谈  发布时间:2020-04-11 13:27:48  来源:网络整理

上海贝越佳园房体结构_西方文学框架体结构_结构体

在iOS开发中,结构是常用的数据类型,它们与指针一样频繁使用,因此需要认真对待.

当一个整体由多个数据组成时,我们可以使用数组来表示整体,但是该数组具有一个特征: 内部的每个元素必须是相同类型的数据. 但是,在实际应用中,我们通常需要根据不同类型的数据形成一个整体. 例如,整个学生的身体可以由姓名,年龄和身高等数据组成. 这些数据具有不同的类型. 名称可以是字符串类型或年龄可以是整数类型,高度可以是浮点类型. 为此,C语言专门提供了一种构造类型来解决上述问题,即结构,它允许内部元素具有不同的类型.

结构内部的元素(即组件)通常称为“成员”.

该结构的一般定义形式为:

struct 结构体名{  
     类型名1 成员名1;
     类型名2 成员名2;
    ……
     类型名n 成员名n;   
 };

例如,我们定义一个学生

struct Student{
    char*name;//姓名
    int age;//年龄
    float height;//身高
};

上面定义了一个名为Student的结构,共有3个成员: 姓名,年龄和身高. 哈哈,看看这里是否有一些面向对象的风格. 实际上结构体,这与面向对象完全不同. 只能说感觉有点相似.

前面只是定义了一个名为Student的结构类型,而不是像int一样的结构变量,只是一个类型.

接下来,有很多方法来定义结构变量.

struct Student{
    char*name;//姓名
    int age;//年龄
    float height;//身高
};
struct Student stu;

西方文学框架体结构_上海贝越佳园房体结构_结构体

定义一个结构变量结构体,变量名是stu. struct和Student一起使用.

struct Student{
    char*name;//姓名
    int age;//年龄
    float height;//身高
}stu;

结构变量名称为stu

struct {
    char*name;//姓名
    int age;//年龄
    float height;//身高
}stu;

结构变量名称为stu

以下方法是错误的,请注意第3行

struct Student {
     int age;
     struct Student stu;
 };

 struct Date {
      int year;
      int month;
      int day;
  };
  
 struct Student {
      char *name;
      struct Date birthday; //................(1)
 };

注意: (1)行

struct Student {
     char *name;
     int age;
 };

// ................在此之前,系统未分配存储空间

上海贝越佳园房体结构_结构体_西方文学框架体结构

struct Student stu; // .........执行6行时,系统将为stu变量分配存储空间.

例如,以下学生结构:

 struct Student {
     char *name; // 姓名
     int age; // 年龄
     float height; // 身高
 };

在16位编译器环境中,Student变量占用的内存总量为: 2 + 2 + 4 = 8字节.

将每个成员的初始值依次放在一对大括号{}中,并用逗号隔开,并逐个分配值.

例如,初始化Student结构变量stu

 struct Student {
     char *name;
     int age;
 };
 struct Student stu = {"MJ", 27};

只能在定义变量时执行初始分配. 初始分配和变量定义不能分开. 以下方法是错误的:

struct Student stu;
stu = {"MJ", 27};

 struct Student {
     char *name;
     int age;
 };
 struct Student stu; 
 stu.age = 27; // 访问stu的age成员........(2)

The

结构体_上海贝越佳园房体结构_西方文学框架体结构

(2)行为结构的年龄成员分配一个值. “. ”被称为成员运算符,在所有运算符中具有最高优先级

  struct Date {
       int year;
       int month;
       int day;
  };
  struct Student {
      char *name;
      struct Date birthday;
 };
 struct Student stu;
 stu.birthday.year = 1986;
 stu.birthday.month = 9;
 stu.birthday.day = 10;

  struct Student {
      char *name;
      int age;
  };
 struct Student stu1 = {"MJ", 27};
 struct Student stu2 = stu1; // 将stu1直接赋值给stu2
 printf("age is %d", stu2.age);

输出为: 年龄为27

像结构变量一样,有3种方法来定义结构数组

(1),

struct Student {
    char *name;
    int age;
};
struct Student stu[5]; //定义1

(2),

struct Student {
    char *name;
    int age;
} stu[5]; //定义2

(3),

struct {
    char *name;
    int age;
} stu[5]; //定义3

上海贝越佳园房体结构_结构体_西方文学框架体结构

以上三种方法都定义了一个结构体数组,其变量名为stu,数组元素个数为5

struct {
    char *name;
    int age;
} stu[2] = { {"MJ", 27}, {"JJ", 30} };

您还可以使用数组下标访问每个结构元素,这与常规数组用法相同

当将结构变量作为函数参数传递时,实际上将传递所有成员的值,即,将实际参数中成员的值分配给相应的形式参数成员. 因此,形式参数的更改不会影响实际参数.

96E7DE2F-5EFE-4B92-A0A4-EE196E126A3D.png

(1)结构变量名称. 会员名

(2),(*指针变量名称). 会员名

(3)指针变量名称->成员名称

38622765-4312-4EEE-8D42-148230B72355.png

输出为:

24170725-fe1c21f2d61e4db1a3ea18223098c21b.png


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-171817-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 更始帝刘玄
        更始帝刘玄

        中国可以在附近海

      热点图片
      拼命载入中...