首页 > 其他 > 详细

结构体

时间:2019-02-15 15:49:25      阅读:141      评论:0      收藏:0      [点我收藏+]

1.typedef

#include <stdio.h>
//typedef是为了已存在的数据类型起别名
//与#define不同的是,#define发生在预处理,typedef发生在编译阶段
typedef unsigned int ui;
typedef unsigned long ul;
int main()
{
    ui a = 10;
    ul b = 110;
    printf("%d\n",a);
    printf("%d\n",b);
    return 0;
}

技术分享图片


 

2.结构体的定义  修改 调用

#include <stdio.h>
#include <string.h>
//结构体名称
struct stu
{
    //编号 姓名 性别 成绩 年龄 地址
    int id;
    char name[21];//一个中文是两个字符
    char sex;
    int age;
    int score;
    char adde[51];
};
int main()
{
    //定义结构体变量
    //数据类型 (struct stu)
    //按照结构体成员列表顺序依次初始化数据
    struct stu on ={01,"巫妖果子",,20,90,"江西抚州东乡区"};
    //打印结构体信息
    printf("学号:%d %s,性别:%c,今年%d岁,平均成绩:%d,籍贯:%s人\n",on.id,on.name,on.sex,on.age,on.score,on.adde);
    //结构体的修改
    on.id=02;
    //字符串拷贝 strcpy(目标字符串,源字符串)
    strcpy(on.name,"果子巫妖");
    printf("学号:%d %s,性别:%c,今年%d岁,平均成绩:%d,籍贯:%s人",on.id,on.name,on.sex,on.age,on.score,on.adde);
    return 0;
}

技术分享图片


3.结构体数组

#include <stdio.h>
//为结构体起别名
typedef struct stu student;
struct stu
{
    //编号 姓名 性别 成绩 年龄 地址
    int id;
    char name[21];//一个中文是两个字符
    char sex;
    int age;
    int score;
    char adde[51];
};

int main()
{
    int i;
    //定义结构体数组 数组名 [元素个数]
    student s[3] =
    {
        {01,"巫妖果子",,20,90,"江西抚州东乡区星港湾"},
        {02,"果子巫妖",,20,80,"江西抚州东乡区东景花苑"},
        {03,"独孤求败",,20,100,"江西抚州东乡区学府世家"}
    };
    //通过结构体数组[下标].成员 找到对应内容
    for(i=0;i<3;i++)
    {
        printf("学号:%d %s,性别:%c,今年%d岁,平均成绩:%d,籍贯:%s\n",
            s[i].id,s[i].name,s[i].sex,s[i].age,s[i].score,s[i].adde);
    }
    return 0;
}

技术分享图片


4.结构体数组的排序

#include <stdio.h>
//为结构体起别名
typedef struct stu
{
    //编号 姓名 性别 成绩 年龄 地址
    int id;
    char name[21];//一个中文是两个字符
    char sex;
    int age;
    int score;
}student;
void BubbleSort(student* s,int len)
{
    int i,j;
    student temp;
    for(i = 0 ;i<len-1;i++)
    {
        for(j = 0 ;j<len-1-i;j++)
        {
            //根据成绩排序 两个相邻结构体比较其它成员
            if(s[j].score<s[j+1].score)
            {
                temp=s[j];
                s[j]=s[j+1];
                s[j+1]=temp;
            }
        }
    }
}
int main()
{
    int i;
    student s [2];
    //通过键盘获取学生信息
    for(i=0;i<2;i++)
    {
        scanf("%d%s%s%d%d\n",&s[i].id,&s[i].name,&s[i].sex,&s[i].age,&s[i].score);
    }
     BubbleSort(s,2);
    for(i=0;i<2;i++)
    {
        printf("学号:%d %s,性别:%s,今年%d岁,平均成绩:%d",
            s[i].id,s[i].name,s[i].sex,s[i].age,s[i].score);
    }    
    return 0;
}

5.结构体赋值

#include <stdio.h>
#include <string.h>

struct stu
{
    char name[21];
    int age;
    char sex;
};

int main()
{
    struct stu s;
    struct stu s1;
    strcpy(s.name,"玉兔");
    s.age=18;
    s.sex=F;

    s1=s;

    printf("姓名:%s\n",s1.name);
    printf("性别:%s\n",s1.sex ==M?"":"");
    printf("年龄:%d\n",s1.age);
    return 0;
}

技术分享图片

 

结构体

原文:https://www.cnblogs.com/zjm1999/p/10383920.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!