2020.1.13 C语言学习 结构体+结构体数组+结构体指针

结构体

结构体的形式

  1. struct 结构体名称
    {
    结构体成员1;
    结构体成员2;

    };
    例如: 图书的结构体
struct Book
{
    char title[128];
    char author[48];
    float price;
    unsigned int date;
    char publisher[40];
};
  1. 定义结构体类型变量
    可以在结构体声明时 后面跟上变量,这个变量是全局变量
    例如
struct Book
{
    char title[128];
    char author[48];
    float price;
    unsigned int date;
    char publisher[40];
}book;//boo是全局变量

也可以在主函数中定义

#include <stdio.h>

struct Book
{
    char title[128];
    char author[48];
    float price;
    unsigned int date;
    char publisher[40];
};

int main(void)
{
    struct Book book;//这个是局部变量
    return 0;
}
  1. 访问结构体变量
    要访问结构体成员,我们需要引入一个新的运算符 —点号(.)运算符。比如book.title就是引用book结构体的title成员, 他是一个字符数组;而book.price 则是引用book结构体的price成员,它是一个浮点型的变量

例如 图书

#include <stdio.h>

struct Book
{
    char title[128];
    char author[48];
    float price;
    unsigned int date;
    char publisher[40];
}book;

int main(void)
{
    printf("请输入书名: ");
    scanf("%s", book.title);
    printf("请输入作者: ");
    scanf("%s", book.author);
    printf("请输入售价: ");
    scanf("%f", &book.price);
    printf("请输入出版日期: ");
    scanf("%d", &book.date);
    printf("请输入出版社: ");
    scanf("%s", book.publisher);

    printf("\n=======数据录入完毕=====\n");

    printf("书名: %s\n", book.title);
    printf("作者: %s\n", book.author);
    printf("售价: %.2f\n", book.price);
    printf("出版日期: %d\n", book.date);
    printf("出版社: %s\n", book.publisher);
    return 0;
}

结果输出

请输入书名: 老人与海
请输入作者: 海明威
请输入售价: 20
请输入出版日期: 20200113
请输入出版社: 中国矿业大学出版社

=======数据录入完毕=====
书名: 老人与海
作者: 海明威
售价: 20.00
出版日期: 20200113
出版社: 中国矿业大学出版社

初始化结构体变量

初始化一个结构体变量:

struct Book book = {
		"老人与海",
		"海明威";
		20,
		20200113
		"中国矿业大学出版社"
};

一定要对号入座

初始化结构体的指定成员值

结构体初始化成员使用点号(.)运算符和成员名
例如:让程序只初始化Book的price成员:

struct Book book = {.price = 20};

也可以不按结构体声明的成员顺序进行初始化:

struct Book book = {
				.publisher = "中国矿业大学出版社",
				.price = 20,
				.date = 20200113
};



结构体嵌套

嵌套例子:

#include <stdio.h>

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

struct Book
{
    char title[128];
    char author[40];
    float price;
    struct Date date;//嵌套
    char publisher[40];
}book = {
    "老人与海",
    "海明威",
    20,
    {2020, 01, 13},
    "中国矿业大学出版社"
};
int main(void)
{
    printf("书名: %s\n", book.title);
    printf("作者: %s\n", book.author);
    printf("价格: %.2f\n", book.price);
    printf("日期: %d-%d-%d\n", book.date.year, book.date.month,
           book.date.day);
    printf("出版社: %s\n", book.publisher);

    return 0;
}

输出结果

书名: 老人与海
作者: 海明威
价格: 20.00
日期: 2020-1-13
出版社: 中国矿业大学出版社

结构体数组

第一种方法是在声明结构体的时候进行定义:

struct 结构体名称
{
	结构体成员1;
	结构体成员2;
	...
}数组名[长度];

第二种方法是先声明一个结构体类型,在用此类型定义一个结构体数组:

struct 结构体名称
{
	结构体成员1;
	结构体成员2;
	...
};
struct 结构体名称 数组名[长度];

初始化结构体数组

例子:

struct Book book[3] = {
	{"老人与海", "海明威", 20, {2020, 01, 13}, "中国矿业大学出版社"};
	{"老人与陆", "陆明威", 21, {2021, 01, 13}, "清华大学出版社"};
	{"老人与空", "空明威", 22, {2022, 01, 13}, "北京大学出版社"};
};

传递结构体变量

  • 结构体变量之间可以通过赋值号传递信息,前提是变量的类型相同;

例子:

#include <stdio.h>

int main(void)
{
    struct Test
    {
        int x;
        int y;
    }t1, t2;

    t1.x = 3;
    t1.y = 4;

    t2 = t1;//赋值号传递
    printf("t2.x = %d, t2.y = %d", t2.x, t2.y);
    return 0;
}

输出结果

t2.x = 3, t2.y = 4

结构体指针

例子:图书录入程序

#include <stdio.h>

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

struct Book
{
    char title[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
};

struct Book getInput(struct Book book);
void printBook(struct Book book);

struct Book getInput(struct Book book)
{
    printf("请输入书名: ");
    scanf("%s", book.title);
    printf("请输入作者: ");
    scanf("%s", book.author);
    printf("请输入售价: ");
    scanf("%f", &book.price);
    printf("请输入出版日期: ");
    scanf("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
    printf("请输入出版社: ");
    scanf("%s", book.publisher);

    return book;

};

void printBook(struct Book book)
{
    printf("书名: %s\n", book.title);
    printf("作者: %s\n", book.author);
    printf("售价: %.2f\n", book.price);
    printf("出版日期: %d-%d-%d\n", book.date.year,book.date.month,book.date.day);
    printf("出版社: %s\n", book.publisher);
}

int main(void)
{
    struct Book b1, b2;
    printf("请录入第一本书的信息...\n");
    b1 = getInput(b1);
    putchar('\n');
    printf("请录入第二本书的信息...\n");
    b2 = getInput(b2);

    printf("\n\n录入完毕,现在开始打印验证...\n\n");
    printf("打印第一本书的信息..\n");
    printBook(b1);
    putchar('\n');
    printf("打印第二本书的信息..\n");
    printBook(b2);

    return 0;
}

输出结果

请录入第一本书的信息...
请输入书名: 老人与海
请输入作者: 海明威
请输入售价: 20
请输入出版日期: 2020-1-13
请输入出版社: 清华大学出版社

请录入第二本书的信息...
请输入书名: 年轻人与海
请输入作者: 海明威
请输入售价: 20
请输入出版日期: 2020-1-13
请输入出版社: 北京大学出版社


录入完毕,现在开始打印验证...

打印第一本书的信息..
书名: 老人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 清华大学出版社

打印第二本书的信息..
书名: 年轻人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 北京大学出版社

现在用结构体指针

还是上面的例子,略作修改

#include <stdio.h>

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

struct Book
{
    char title[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
};

void getInput(struct Book *book);
void printBook(struct Book *book);

void getInput(struct Book *book)
{
    printf("请输入书名: ");
    scanf("%s", book->title);
    printf("请输入作者: ");
    scanf("%s", book->author);
    printf("请输入售价: ");
    scanf("%f", &book->price);
    printf("请输入出版日期: ");
    scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
    printf("请输入出版社: ");
    scanf("%s", book->publisher);
};

void printBook(struct Book *book)
{
    printf("书名: %s\n", book->title);
    printf("作者: %s\n", book->author);
    printf("售价: %.2f\n", book->price);
    printf("出版日期: %d-%d-%d\n", book->date.year,book->date.month,book->date.day);
    printf("出版社: %s\n", book->publisher);
}

int main(void)
{
    struct Book b1, b2;
    printf("请录入第一本书的信息...\n");
    getInput(&b1);
    putchar('\n');
    printf("请录入第二本书的信息...\n");
    getInput(&b2);

    printf("\n\n录入完毕,现在开始打印验证...\n\n");
    printf("打印第一本书的信息..\n");
    printBook(&b1);
    putchar('\n');
    printf("打印第二本书的信息..\n");
    printBook(&b2);

    return 0;
}

输出结果

打印第一本书的信息..
书名: 老人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 清华大学出版社

打印第二本书的信息..
书名: 年轻人与海
作者: 海明威
售价: 20.00
出版日期: 2020-1-13
出版社: 北京大学出版社
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值