c语言结构体和联合体例题

本文提供了一系列C语言结构体和联合体的编程实例,包括定义图书信息结构、添加显示、初始化、输入图书信息的函数,以及涉及汽车信息、学生信息和图书数组的操作。此外,还展示了如何利用结构体进行排序、输入输出和数学运算操作。

第一题:
要求你设计一个能够保存图书信息的结构。图书属性包括:书名(title)、作者(author)和单价信息(

price),并按照下面要求完成对于各种图书的相关操作。
/*
 struct books {
 char title[100];
 char author[20];
 double price;
 } doyle = { "My life as a budgie", "Mack Tom", 14.6 };
 int main(void) {
 struct books dicken = { "Thinking in C++", "Stephen Prata", 78 };
 struct books panshin = { .title = "C++ Primer", .author = "Stanley Lippman",
 .price = 92.5 };

 printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
 doyle.title, doyle.author, doyle.price);
 printf("\n");
 printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
 dicken.title, dicken.author, dicken.price);
 printf("\n");
 printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
 panshin.title, panshin.author, panshin.price);
 printf("\n");
 printf("“Thinking in C++”这本书的价格调整后为:\n");
 printf("\n");
 printf("The title is :%s\nThe author is :%s\nThe price is :%lf\n",
 dicken.title, dicken.author, dicken.price = 85);
 return EXIT_SUCCESS;
 }
 */

第二题:
为上面的关于图书的程序,添加三个函数:
/*(1)编写显示图书信息函数show()。参数为结构的指针。显示图书信息的结构如下:
The title is :My life as a budgie
The author is :Mack Tom
The price is :14.6

#include <stdio.h>
#include <stdlib.h>

struct Library {
 const char title[20];
 const char author[10];
 double price;
} panshin;

void show(struct Library *doy) {

 printf("The title is: %s\n The author is : %s\n The price is : %.1lf",doy->title,

doy->author, doy->price);
}

int main(void) {

 struct Library doyle = { "My life as a budgie", "Mack Tom", 14.6 };
 show(&doyle);
 return EXIT_SUCCESS;
}*/
/*(2)编写初始化结构变量函数init(),参数为结构的指针。函数功能是将结构变量中的成员进行初始化。

#include <stdio.h>
#include <stdlib.h>

struct Library {
 const char title[20];
 const char author[10];
 double price;
} panshin;

void init(struct Library *doyle, struct Library *dicken, struct Library *panshin) {
 doyle ->title;
 dicken ->author;
 panshin ->price;

}
int main(void) {

 struct Library doyle = { "My life as a budgie", "Mack Tom", 14.6 };
 struct Library dicken ={ "Thinking in C++", "Stephen Prata", 78 };
 struct Library panshin = { "C++ Prinner", "Stanley Lippman", 92.5 };
 init(&doyle,&dicken,&panshin);
 printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
    doyle->title, doyle->author, doyle->price);
 printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
    dicken->title, dicken->author, dicken->price);
 printf("The title is: %s\n The author is : %s\n The price is : %.1lf",
    panshin->title, panshin->au

### 关于C语言结构体的笔试题目 以下是几个典型的C语言结构体笔试题目及其解析: #### 题目一 **已知如下代码,预测其运行结果:** ```c #include <stdio.h> struct Test { char a; int b; }; int main() { printf("Size of struct Test: %lu\n", sizeof(struct Test)); return 0; } ``` **解答:** 在大多数平台上,`char` 占用1字节,`int` 占用4字节。由于内存对齐的原因,在某些编译器下,该结构体会被填充到满足最大成员(即 `int` 的4字节边界)。因此,此结构体的实际大小通常是8字节[^1]。 --- #### 题目二 **分析以下两段代码的结果差异:** ```c // Code Segment 1 struct Example1 { char c1; char c2; int i; }; printf("%zu\n", sizeof(struct Example1)); // Code Segment 2 struct Example2 { char c1; int i; char c2; }; printf("%zu\n", sizeof(struct Example2)); ``` **解答:** 对于 `Example1`,两个连续的 `char` 成员不会引入额外的填充,随后的 `int` 对齐至4字节边界,最终大小为8字节。而对于 `Example2`,第二个 `char` 后面可能需要3字节填充以使后续的 `int` 能够正确对齐,总大小变为12字节[^5]。 --- #### 题目三 **给定以下定义,请计算 `sizeof(struct Data)` `sizeof(union UData)` 的值:** ```c typedef union { long l; int k[5]; char c; } UnionType; struct StructType { int m; UnionType u; double d; }; ``` **解答:** - **UnionType**: 联合体内存分配取决于最大的数据类型,这里是数组 `k[5]`,占用20字节。 - **StructType**: 结构体中的联合体占据20字节空间,加上前后其他字段以及必要的填充,整个结构体大小为32字节[^2]。 --- #### 题目四 **如何初始化一个具有多个成员的复杂结构体?** ```c struct Complex { int id; float value; char name[20]; }; int main() { struct Complex obj = { .id = 1, .value = 3.14f }; // 或者更完整的初始化方式 struct Complex fullObj = { 2, 2.718f, "Sample Name" }; return 0; } ``` **解答:** 可以利用指定初始值的方式逐一赋值,也可以一次性提供所有成员的数据。注意字符串常量会被自动复制到字符数组中[^3]。 --- #### 题目五 **判断下列说法是否正确,并解释原因:如果未显式初始化结构体,则默认值全为零。** **解答:** 错误。仅当全局或静态声明时,默认初始化为零;局部变量则含有垃圾值,除非手动设置[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值