gpt4 book ai didi

c - 在 C 中使用 struct 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-02 09:13:54 26 4
gpt4 key购买 nike

我知道 C 中的结构是数据的集合,但我不确定是否可以通过以下方式操作它:-

假设我有如下内容:

typedef struct { 
char id[IDSIZE];
name name;
int score;
} record;

如果我想记录数据,我可以简单地写一行:

sscanf(line, "%s %s %d", &record);

将该行存储到记录本身?

最佳答案

record 是您的类型名称。如intchar。所以你必须声明一个 record 类型的变量。

record currentRecord;

sscanf 每种格式标记都需要一个变量地址

sscanf(line, "%s %s %d", &currentRecord); //Fail
sscanf(line, "%s %s %d", &currentRecord.id, &currentRecord.name, &currentRecord.score); //This will work only if name is type of char*

name name 不起作用。变量和类型名称必须不同。

关于c - 在 C 中使用 struct 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49088163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com