鱼C论坛

 找回密码
 立即注册
查看: 1140|回复: 13

[已解决]结构体初学问题

[复制链接]
发表于 2021-4-14 10:35:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

struct Martial
{
    int id; //门派id
    char name[50];//门派名称
    char count;//门派人数
    char type; //门派类别 1正派 2中立 3邪派
}martial = {1,"德玛西亚",100,1};
struct player
{
    int id;//玩家的id
    char name[50];//玩家的名称
    char pass[50];//玩家的密码
    char sex;//玩家性别
    struct Martial martial;//玩家所属门派
};
int main()
{
    struct player player = {1,"小彪",1236,'f'};
    printf("%s\n",player.martial.name);
    return 0;
}
最后提取不出来门派的名称
最佳答案
2021-4-14 11:55:12

c/c++编译器不报错的多了,结构体中的结构体不能像你这样直接赋值,它找不到的...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-14 11:22:26 | 显示全部楼层

回帖奖励 +2 鱼币

本帖最后由 yuxijian2020 于 2021-4-14 11:28 编辑

因为你没有给player结构体中的门派赋值

最好是重新弄个方法用于给player对象赋值
void InitPlayer(struct player* p, int id, char* name, char* pass, char sex, int mId, char* mName, char count, char type)
{
    p->id = id;
    strcpy_s(p->name, sizeof(p->name) / sizeof(char), name);
    strcpy_s(p->pass, sizeof(p->pass) / sizeof(char), pass);
    p->sex = sex;

    p->martial.id = mId;
    strcpy_s(p->martial.name, sizeof(p->martial.name) / sizeof(char), mName);
    p->martial.count = count;
    p->martial.type = type;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 11:53:14 | 显示全部楼层
yuxijian2020 发表于 2021-4-14 11:22
因为你没有给player结构体中的门派赋值

最好是重新弄个方法用于给player对象赋值

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

struct Martial
{
    int id; //门派id
    char name[50];//门派名称
    char count;//门派人数
    char type; //门派类别 1正派 2中立 3邪派
};
struct player
{
    int id;//玩家的id
    char name[50];//玩家的名称
    char pass[50];//玩家的密码
    char sex;//玩家性别
    struct Martial martial;//玩家所属门派
};
int main()
{
    struct player player = {1,"小彪",1236,'f',{1,"德玛西亚",100,1}};
    printf("%s\n",player.martial.name);
    return 0;
}
可就算我给他赋值也是提取不出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 11:53:49 | 显示全部楼层
yuxijian2020 发表于 2021-4-14 11:22
因为你没有给player结构体中的门派赋值

最好是重新弄个方法用于给player对象赋值

编译器也没报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-14 11:55:12 | 显示全部楼层    本楼为最佳答案   

c/c++编译器不报错的多了,结构体中的结构体不能像你这样直接赋值,它找不到的...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-14 11:59:41 | 显示全部楼层

忘记说了,你这种写法在我vs2019上是报错的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-14 12:01:32 | 显示全部楼层

回帖奖励 +2 鱼币

yuxijian2020 发表于 2021-4-14 11:59
忘记说了,你这种写法在我vs2019上是报错的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Martial
{
    int id; //门派id
    char name[50];//门派名称
    char count;//门派人数
    char type; //门派类别 1正派 2中立 3邪派
};
struct player
{
    int id;//玩家的id
    char name[50];//玩家的名称
    char pass[50];//玩家的密码
    char sex;//玩家性别
    struct Martial martial;//玩家所属门派
};
int main()
{
    struct player player = {1,"小彪","1236",'f',{1,"德玛西亚",100,1}};
    printf("%s\n",player.martial.name);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 12:02:57 | 显示全部楼层
yuxijian2020 发表于 2021-4-14 11:59
忘记说了,你这种写法在我vs2019上是报错的

好的  谢谢解答
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 12:03:43 | 显示全部楼层

我换一种写法  指针还不是太熟练
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 12:46:28 | 显示全部楼层

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

struct Martial
{
    int id; //门派id
    char name[50];//门派名称
    char count[50];//门派人数
    char type; //门派类别 1正派 2中立 3邪派
};
struct player
{
    int id;//玩家的id
    char name[50];//玩家的名称
    char pass[50];//玩家的密码
    char sex;//玩家性别
    struct Martial martial;//玩家所属门派
};
int main()
{
    struct player player = {1,"小彪","123456",'f'};
    player.martial.id = 1;
    strcpy(player.martial.name,"天山");
    strcpy(player.martial.count,"300");
    player.martial.type = 3;
    printf("%s\n",player.martial.name);
    return 0;
}
换了种赋值方式总算出来了
你的解答我学到后面会再看一看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-14 13:05:40 | 显示全部楼层
天敌521 发表于 2021-4-14 12:46
#include
#include
#include

你看一下7楼的回答,  你是赋值的时候  pass是字符串但是你赋值成int了,所以才报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-14 13:06:47 | 显示全部楼层

我眼瞎了  我就说咋能报错呢,他属性赋值就不对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 18:22:50 | 显示全部楼层

感谢感谢 粗心了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 18:30:35 | 显示全部楼层
yuxijian2020 发表于 2021-4-14 13:05
你看一下7楼的回答,  你是赋值的时候  pass是字符串但是你赋值成int了,所以才报错

感谢感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 17:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表