c - C 中带数字的结构名称

标签 c structure

我是 C 新手,我正在尝试创建一个结构来管理书籍:

typedef struct bookManagement{
    char *name;
    char *author;
    char *country;
    char *genre;
    int year;
} book;

如何创建带有数字的结构名称,例如:

main(){
book 1={"The great Gatsby","F. Scott Fitzgerald","USA","novel",1925};
}

我的 IDE 报告了此语句的错误:

"[Error] expected identifier or '(' before numeric constant"

最佳答案

来自 Kernighan 和 Ritchie 的《C 编程语言》,第 35 页:

There are some restrictions on the names of variables and symbolic constants. Names are made up of letters and digits; the first character must be a letter.

所以你想要的是这样的:

typedef struct bookManagement{
    char *name;
    char *author;
    char *country;
    char *genre;
    int year;
} book;

int main(){
    book book1={"The great Gatsby","F. Scott Fitzgerald","USA","novel",1925};
}

如果你想对数字进行某种索引,我想你会想要使用结构数组。如果您需要更多信息,我建议您阅读 this guide因为它为可以完成的方法提供了一些不同的选项。祝学习顺利!

关于c - C 中带数字的结构名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37638639/

相关文章:

c - 这是 FPS 独立游戏循环的良好实现吗?

c - XML 验证器(检查给定的 xml 是否正确或损坏)

c++ - 继承还是包容? C++

c++ - 如何将多组数据存储到一个结构数组中?

c - 释放二维字符数组

c - 将一个数组中的元素移动/移除到另一个数组中

c - MSP430 在 UP 模式下终止 timer_A

c - 一个结构存储在另一个结构中

c++ - 结构原型(prototype)?

C - fscanf 错误与 char *