黑马程序员————结构体

            ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一、什么是结构体

对于数组中,每个元素都是相同的,如果我们想使每个元素不同的话,我们可以考虑使用结构体。
 结构体:结构体可以由多种不同类型的数据类型组成的新的数据类型。
数组:只能由多个相同类型的数据构成。

二、结构体类型的定义

格式:
struct 结构体名 {
        数据类型 变量1;
        数据类型 变量2;
数据类型 变量 3 ;
        ... ...
}

例如:

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


三、结构体变量的定义

1、先定义结构体,再定义变量
    struct Student {
        int age;
        char *name;
    };
    struct Student stu;

2、定义结构体的同时定义变量
    struct Student {
        int age;
        char *name;
    } stu;

3、直接定义结构体变量 (省略类型名称)
    struct {
        int age;
        char *name;
    } stu;

四、结构体的初始化

1、声明变量的同时初始化
    struct Student {
        int age;
        char *name;
    };
    struct Student stu = {22, "hello"};

2、先声明变量,再逐一初始化
    struct Student stu;
    stu.age = 22;
    stu.name = "hello";

3、定义结构体的同时进行变量定义和初始化
    struct Student {
        int age;
        char *name;
    } stu = {22, "hello"};

4、非顺序初始化
    struct Student stu = {.name = "hello", .age = 22};

5.注意:
  •  如果没有初始化结构体,所有变量会自动的有默认值
  • 定义结构体类型,不会分配存储空间
  • 定义结构体变量真正的分配存储空间
  • 结构体类型不能重复定义,类型不能重复
例如:
struct student
{
    int age;              //定义类型的同时定义变量
    char *name;
} stu;



struct student
{
    int age;
    char *name;
}  stu2;
//会报错,类型不能重复定义

五、访问结构体

1、结构体变量是基本数据类型
    int age = stu.age;
    char *name = stu.name;


2、结构体变量是结构体
    struct Date {
        int year;
        int month;
        int day;
    };
    
    struct Student {
        int age;
        char *name;
        struct Date birthday;
    };
    
    struct Student stu = {22, "hello", {1999, 10, 10}};
    int year = stu.birthday.year;
    int month = stu.birthday.month;
    int day = stu.birthday.day;


 
 
3、相同结构体类型变量间可以整体赋值
    struct Student stu1 = {23, "hello"};
    struct Student stu2 = stu1;     // 把结构体变量stu1的值对应的赋值给stu2,所以stu2 = {23, "hello"}

六、结构体数组

1、三种定义方式:
    // 方式一
    struct Student {
        int age;
        char *name;
    } stus[3];
    
    // 方式二
    struct Student {
        int age;
        char *name;
    };
    struct Student stu[3];
    
    // 方式三
    struct {
        int age;
        char *name;
    } stus[3];

2、初始化

和数组的初始化一样。

stus = {{12, "hello"}, {23, "world"}, {24, "welcome"}}

七、结构体指针

1、格式
struct 结构体名 *指针名;
2、访问结构体的方式增加了
之前访问结构体的方式是
结构体变量名.成员变量名
现在增加了
(*指针名).成员变量名
指针名->成员变量名

3、例子
    struct Student {
        int age;
        char *name;
    };
   
    struct Student stu = {23, "hello"};
    struct Student *p = &stu;
    
    printf("%d, %s\n", stu.age, stu.name);
    printf("%d, %s\n", (*p).age, (*p).name);
    printf("%d, %s\n", p->age, p->name);

八、结构体与函数

1、结构体作为函数参数

结构体实参会把成员变量值对应的赋值给函数结构体参数对应的变量值,改变函数结构体参数不会影响到实参。

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

void test(struct Student s)
{
    // 对结构体s的操作不会影响到stu
    s.age = 10;
    s.name = "world";
}

int main()
{
    struct Student stu = {23, "hello"};
    test(stu);
    printf("%d, %s\n", stu.age, stu.name);
    
    return 0;
}

2、结构体指针作为函数
struct Student {
    int age;
    char *name;
};

void test(struct Student * s)
{
    // 对结构体s的操作会影响到stu
    s->age = 10;
    s->name = "world";
}

int main()
{
    struct Student stu = {23, "hello"};
    test(&stu);
    printf("%d, %s\n", stu.age, stu.name);
    
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值