#include<stido.h>
#include<string.h>
//英雄结构体的创建
typedef struct hero {
char name;
int age;
char sex;
}hr;
//冒泡排序,按照英雄年龄的升序排序
void bubblesort(hr *h1, int len)
{
for (int i = 0; i < len-1; i++) {
for (int j = 0; j < len - 1 - i;j++)
{
if (h1[j].age > h1[j + 1].age) //英雄的年龄用来判断
{
//单个英雄的整体数组信息进行排序
hr temp = h1[j];
h1[j] = h1[j+1];
h1[j + 1] = temp;
}
}
}
}
//英雄打印的输出
void printt(hr* h1,int len)
{
for (int i = 0; i < len; i++) {
printf("英雄的姓名:%c,性别:%c 年龄:%d\n",h1[i].name,h1[i].sex,h1[i].age );
}
}
int main() {
hr h1[5]{
{'刘备',23,'男'},
{'关羽',22,'男'},
{'张飞',20,'男'},
{'赵云',21,'男'},
{'貂蝉',19,'男'}
};
bubblesort(h1,sizeof(h1)/sizeof(h1[0]));
printt(h1, sizeof(h1) / sizeof(h1[0]));
}
输出结果
有大佬知道为什么会这样和如何解决么😥
结构体中
typedef struct hero {
char name;
int age;
char sex;
}hr;
char name;应该用字符数组,改成char name[20];
char sex;应该用字符数组,改成char sex[6];
下面赋值的地方:
hr h1[5]{
{'刘备',23,'男'}, 刘备和男,都需要用双引号,而不是用单引号:
hr h1[5]{
{"刘备",23,"男"},
其他的一样
printf中的%c改成%s
代码修改如下:
#include<stdio.h>
#include<string.h>
//英雄结构体的创建
typedef struct hero {
char name[20];
int age;
char sex[6];
}hr;
//冒泡排序,按照英雄年龄的升序排序
void bubblesort(hr *h1, int len)
{
for (int i = 0; i < len-1; i++) {
for (int j = 0; j < len - 1 - i;j++)
{
if (h1[j].age > h1[j + 1].age) //英雄的年龄用来判断
{
//单个英雄的整体数组信息进行排序
hr temp = h1[j];
h1[j] = h1[j+1];
h1[j + 1] = temp;
}
}
}
}
//英雄打印的输出
void printt(hr* h1,int len)
{
for (int i = 0; i < len; i++) {
printf("英雄的姓名:%s,性别:%s 年龄:%d\n",h1[i].name,h1[i].sex,h1[i].age );
}
}
int main()
{
hr h1[5]={
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"男"}
};
bubblesort(h1,sizeof(h1)/sizeof(h1[0]));
printt(h1, sizeof(h1) / sizeof(h1[0]));
return 0;
}
结构体中的声明用: const char *name
printt() 中的printf( 用: %s ....
#include<stdio.h>
#include<string.h>
//英雄结构体的创建
typedef struct hero {
const char *name;
int age;
const char *sex;
}hr;
//冒泡排序,按照英雄年龄的升序排序
void bubblesort(hr *h1, int len)
{
for (int i = 0; i < len-1; i++) {
for (int j = 0; j < len - 1 - i;j++)
{
if (h1[j].age > h1[j + 1].age) //英雄的年龄用来判断
{
//单个英雄的整体数组信息进行排序
hr temp = h1[j];
h1[j] = h1[j+1];
h1[j + 1] = temp;
}
}
}
}
//英雄打印的输出
void printt(hr* h1,int len)
{
for (int i = 0; i < len; i++) {
printf("英雄的姓名:%s,性别:%s 年龄:%d\n",h1[i].name,h1[i].sex,h1[i].age );
}
}
int main() {
hr h1[5] {
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}};
bubblesort(h1,sizeof(h1)/sizeof(h1[0]));
printt(h1, sizeof(h1) / sizeof(h1[0]));
}
英雄的姓名:貂蝉,性别:女 年龄:19
英雄的姓名:张飞,性别:男 年龄:20
英雄的姓名:赵云,性别:男 年龄:21
英雄的姓名:关羽,性别:男 年龄:22
英雄的姓名:刘备,性别:男 年龄:23
Process exited after 0.3354 seconds with return value 0
请按任意键继续. . .
修改如下,供参考:
#include<stdio.h> //#include<stido.h> 修改
#include<string.h>
//英雄结构体的创建
typedef struct hero {
char name[16]; //修改
int age;
char sex[4]; //修改
}hr;
//冒泡排序,按照英雄年龄的升序排序
void bubblesort(hr *h1, int len)
{
for (int i = 0; i < len-1; i++) {
for (int j = 0; j < len - 1 - i;j++)
{
if (h1[j].age > h1[j + 1].age) //英雄的年龄用来判断
{
//单个英雄的整体数组信息进行排序
hr temp = h1[j];
h1[j] = h1[j+1];
h1[j + 1] = temp;
}
}
}
}
//英雄打印的输出
void printt(hr* h1,int len)
{
for (int i = 0; i < len; i++) {
printf("英雄的姓名:%s,性别:%s 年龄:%d\n",h1[i].name,h1[i].sex,h1[i].age );
//printf("英雄的姓名:%c,性别:%c 年龄:%d\n",h1[i].name,h1[i].sex,h1[i].age );
}
}
int main()
{
hr h1[5]={ //修改
{"刘备",23,"男"}, //修改
{"关羽",22,"男"}, //修改
{"张飞",20,"男"}, //修改
{"赵云",21,"男"}, //修改
{"貂蝉",19,"男"} //修改
};
bubblesort(h1,sizeof(h1)/sizeof(h1[0]));
printt(h1, sizeof(h1) / sizeof(h1[0]));
return 0;
}