C语言 结构体、结构体变量和结构体指针

执行结果截图:

 

 代码:

#include "stdio.h"
#include "stdlib.h"

# define PRINTF(templt, ...) fprintf(stderr, templt, ##__VA_ARGS__)
# define PRINT(format, ...) printf(# format, ##__VA_ARGS__)

// 定义一个宏,求两个内存单元地址之间的距离
# define ABS(x, y) (((long long int)x - (long long int)y) > 0 ? ((long long int)x - (long long int)y) : - ((long long int)x - (long long int)y))
struct Date
{
int year;
int month;
int day;
} date;

struct Book
{
char title[120];
char author[40];
float price;
struct Date date;
char publisher[40];
} book = {
"《陆王》",
"威漫",
9.87,
2022,
4,
1,
"山姆大叔出版社"
};

int main(void)
{
// 定义一个结构体指针pt
struct Book * pt;
struct Book * pt1;
struct Book * pt2;
long long int addrDistance;

pt = &book;
PRINTF("Addr of book :%p\n", pt);

PRINT(\n=====打印结构体book的变量值=====\n);
PRINT(书名 : %s\n, book.title);
PRINT(作者 : %s\n, book.author);
PRINT(售价 : %.2f\n, book.price);
PRINT(出版日期 : %d-%d-%d\n, book.date.year, book.date.month, book.date.day);
PRINT(出版社 : %s\n, book.publisher);

/*
// 输入结构体变量的值并打印
PRINT(请输入书名 :);
scanf("%s", book.title);

PRINT(请输入作者 :);
scanf("%s", book.author);

PRINT(请输入售价 :);
scanf("%f", &book.price);

PRINT(请输入出版日期(按年月日依次输入):);
scanf("%d", &book.date.year);
scanf("%d", &book.date.month);
scanf("%d", &book.date.day);

PRINT(请输入出版社 :);
scanf("%s", book.publisher);

PRINT(\n=====录入的数据为=====\n);
PRINT(书名 : %s\n, book.title);
PRINT(作者 : %s\n, book.author);
PRINT(售价 : %.2f\n, book.price);
PRINT(出版日期 : %d-%d-%d\n, book.date.year, book.date.month, book.date.day);
PRINT(出版社 : %s\n, book.publisher);
*/

PRINT(\n=====对结构体变量进行初始化并直接打印其值=====\n);
// 初始化一个结构体变量,可以不按结构体声明的成员顺序进行初始化:
struct Book kungFuBible =
{
.publisher = "功夫宝典出版社",
.title = "《葵花宝典》",
.author = "前朝太监",
.price = 999.99,
.date = {2012, 12 , 12}
};

PRINT(书名 : %s\n, kungFuBible.title);
PRINT(作者 : %s\n, kungFuBible.author);
PRINT(售价 : %.2f\n, kungFuBible.price);
PRINT(出版日期 : %d-%d-%d\n, kungFuBible.date.year, kungFuBible.date.month, kungFuBible.date.day);
PRINT(出版社 : %s\n, kungFuBible.publisher);

// 初始化一个结构体变量,可以不按结构体声明的成员顺序进行初始化:
struct Book compBook =
{
.publisher = "社会大学出版社",
.title = "《C语言-平凡之路》",
.author = "鲁有脚",
.price = 66.66,
.date = {2021, 5, 1}
};

PRINT(\n=====对结构体指针以*解引用打印结构体变量的值=====\n);
pt1 = &compBook;
PRINTF("pt1 pointed to &compBook : %p\n", pt1);
PRINTF("Addr of compBook.title : %p which is %lld away from &compBook\n", &((* pt1).title), ABS(&((* pt1).title), pt1));
PRINT((* pt1).title : %s\n, (* pt1).title);
PRINTF("Addr of (* pt1).author : %p which is %lld away from &compBook\n", &((* pt1).author), ABS(&((* pt1).author), pt1));
PRINT((* pt1).author : %s\n, (* pt1).author);
PRINTF("Addr of (* pt1).price : %p which is %lld away from &compBook\n", &((* pt1).price), ABS(&((* pt1).price), pt1));
PRINT((* pt1).price : %.2f\n, (* pt1).price);
PRINTF("Addr of (* pt1).date.year : %p which is %lld away from &compBook\n", &((* pt1).date.year), ABS(&((* pt1).date.year), pt1));
PRINT((* pt1).date : %d-%d-%d\n, (* pt1).date.year, (* pt1).date.month, (* pt1).date.day);
PRINTF("Addr of (* pt1).publisher : %p which is %lld away from &compBook\n", &((* pt1).publisher), ABS(&((* pt1).publisher), pt1));
PRINT((* pt1).publisher : %s\n, (* pt1).publisher);

PRINT(\n=====用结构体指针指向结构体变量打印结构体变量的值=====\n);
pt2 = &kungFuBible;
PRINTF("pt2 pointed to &kungFuBible: %p\n", pt2);
PRINTF("Addr of kungFuBible.title : %p\n", &(pt2->title));
PRINT(pt2 -> title : %s\n, pt2->title);
PRINT(pt2 -> author : %s\n, pt2->author);
PRINT(pt2 -> price : %.2f\n, pt2->price);
PRINT(pt2 -> date : %d-%d-%d\n, pt2->date.year, pt2->date.month, pt2->date.day);
PRINT(pt2 -> publisher : %s\n, pt2->publisher);

PRINTF("\n=====结构体和结构体变量所占字节数=====\n");
PRINTF("size of memory cell address : %d\n", sizeof(&book));
PRINTF("size of (struct Book *) type : %d\n", sizeof(struct Book *));
PRINTF("size of (struct Book) type : %d\n", sizeof(struct Book));
PRINTF("size of kungFuBible : %d\n", sizeof(kungFuBible));
PRINTF("size of kungFuBible.title : %d\n", sizeof(kungFuBible.title));
PRINTF("size of kungFuBible.author : %d\n", sizeof(kungFuBible.author));
PRINTF("size of kungFuBible.price : %d\n", sizeof(kungFuBible.price));
PRINTF("size of kungFuBible.date : %d\n", sizeof(kungFuBible.date));
PRINTF("size of kungFuBible.publisher : %d\n", sizeof(kungFuBible.publisher));

PRINTF("Address distance of book and compBook are %lld bytes apart\n", ABS(&book, &compBook));
PRINTF("Address distance of book and kungFuBible are %lld bytes apart\n", ABS(&book, &kungFuBible));
PRINTF("Address distance of compBook and kungFuBible are %lld bytes apart\n", ABS(&compBook, &kungFuBible));

return 0;
}
posted @   JohnnyH  阅读(293)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示