C语言结构体 里的字符串比较

Posted on   Astro_Leon  阅读(384)  评论(0编辑  收藏  举报
复制代码
#include<stdio.h>
#include<string.h>
//10.04
struct student
{
    char name[10];
    int num;
    int score;
};
int main()
{
    struct student stu[5] = { {"bangyan",1,703},{"angyan",1,723}, {"cgyan",1,603}, {"gyan",1,753}, {"wyan",1,537} };

    struct student temp;
    for (int i = 0; i < 5; i++)
    {
        for (int j = i; j < 5; j++)
        {
            if (strcmp(stu[j].name, stu[i].name)>0)//按照从大到小的顺序排列    //第一次写的时候犯了一个致命的错误  以为负数算假  所以一直比较错 负数算真
            {
                //printf("%d  ",strcmp("asd","asd"));
                temp = stu[i];
                stu[i] = stu[j];
                stu[j] = temp;
            }
        }
    }
    for (int i = 0; i < 5; i++)
        printf("%s\n", stu[i].name);

    return 0;
}
复制代码

负数算真    只有零算假,所有的非负数都算作真

努力加载评论中...
点击右上角即可分享
微信分享提示