c语言 结构体(及相关例题)

本文详细介绍了如何使用struct语句定义结构体,包括初始化结构体变量的方法,以及通过实例演示了如何在C语言中实现结构体数组的初始化,查找结构体数组中特定元素的过程,如年龄最大者和成绩最高者,还展示了如何对结构体数组进行排序。

定义结构
为了定义结构,您必须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型,struct 语句的格式如下:
struct Student
{
int sno;
char name[20];
char cname[20];

}stu;
Student,是结构体标签.
stu结构变量,定义在结构的末尾,最后一个分号之前,您可以指定一个或多个结构变量.
结构体变量的初始化
和其它类型变量一样,对结构体变量可以在定义时指定初始值。

#include <stdio.h>
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book = {"C 语言", "RUNOOB", "编程语言", 123456};
 
int main()
{
    printf("title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n", book.title, book.author, book.subject, book.book_id);
}

输出结果:
title : C 语言
author: RUNOOB
subject: 编程语言
book_id: 123456

例题:
输出结构体数组中年龄最大者的数据 (10 分)
给定程序中,函数fun的功能是:将形参std所指结构体数组中年龄最大者的数据作为函数值返回,并在主函数中输出。

函数接口定义:
struct student fun(struct student std[], int n);
其中 std 和 n 都是用户传入的参数。 函数fun的功能是将含有 n 个人的形参 std 所指结构体数组中年龄最大者的数据作为函数值返回。

裁判测试程序样例:
#include <stdio.h>

struct student
{char name[10];
int age;
};
struct student fun(struct student std[], int n);
int main()
{
struct student std[5]={“aaa”,17,“bbb”,16,“ccc”,18,“ddd”,17,“eee”,15 };
struct student max;
max=fun(std,5);
printf("\nThe result:\n");
printf("\nName : %s, Age : %d\n", max.name,max.age);
return 0;
}

/* 请在这里填写答案 */
输出样例:

The result:

Name : ccc, Age : 18

struct student fun(struct student  std[], int  n)
{
  struct student max;
  int i;
  max=*std;
  for(i=0;i<n;i++)
  {
      if(max.age<std[i].age)
        max=std[i];
  }
  return max;

}

7-2 查找成绩最高的学生 (10 分)
编写程序,从键盘输入 n (n<10)个学生的学号(学号为4位的整数,从1000开始)、成绩并存入结构数组中,查找并输出成绩最高的学生信息。

输入输出示例:括号内为说明,无需输入输出

输入样例:
3 (n=3)
1000 85
1001 90
1002 75
输出样例:
1001 90

#include <stdio.h>
#define  N  8

typedef struct student
{
   int no;
   int score;
}student;

int main()
{
    student s[1000];
    int n,i;
    int max;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&s[i].no);
        scanf("%d",&s[i].score);
        max=s[0].score;
        if(max<s[i].score)
            max=s[i].score;
    }
    for(i=0;i<n;i++)
    {
        if(s[i].score==max)
            printf("%d ",s[i].no);
    }
        printf("%d",max);
    return 0;
 }

学生排序 (10 分)
编写程序,从键盘输入 n (n<10)个学生的学号(学号为4位的整数,从1000开始)、成绩并存入结构数组中,按成绩从低到高排序并输出排序后的学生信息。

输入输出示例:括号内为说明,无需输入输出

输入样例:
3 (n=3)
1000 85
1001 90
1002 75
输出样例:
1002 75
1000 85
1001 90

#include <stdio.h>
#define  N  8

typedef struct student
{
   int no;
   int score;
}student;

int main()
{
    student s[1000];
    int n,i,j;
    int temp;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&s[i].no,&s[i].score);
    }
    for(i=0;i<n;i++)
    {
       for(j=0;j<n-1;j++)
       {
           if(s[j].score>s[j+1].score)//对分数排序
           {
             temp=s[j].score;
             s[j].score=s[j+1].score;
             s[j+1].score=temp;

            //分数排序完毕后,学号也要交换
             temp=s[j].no;
             s[j].no=s[j+1].no;
             s[j+1].no=temp;
           }
       }

    }
    for(i=0;i<n;i++)
    {
            printf("%d %d\n",s[i].no,s[i].score);
    }
    return 0;
 }
### 关于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]。 --- ###
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值