c - 大量结构错误

标签 c struct

这是一段极其简单且无用的练习代码,我正在开始看起来像是极其无用的书中。我正在做一个结构练习,在代码编译时我收到了一些错误。 这是有问题的代码:

struct fish = {
    const char *name;
    const char *species;
    int teeth;
    int age;
};


void catalog(struct fish f)
{
    printf("%s is a %s with %i teeth. He is %i.\n", f.name, f.species, f.teeth, f.age);
}


int main()
{
struct fish snappy = {"Snappy", "piranha", 69, 4};

catalog(snappy);
return 0;
}

这是书中的准确代码,减去目录上面的结构定义。我最终只是复制粘贴,因为我开始怀疑这本书完全错误。该书声称上述代码应该在没有定义结构的情况下编译和运行。我尝试将结构定义放入头文件中,并且尝试删除它或将其添加到代码的不同部分。我得到了同样的错误:

snappy.c:8:13: error: expected identifier or ‘(’ before ‘=’ token
struct fish = {
         ^
snappy.c:16:26: error: parameter 1 (‘f’) has incomplete type
 void catalog(struct fish f)
                      ^
snappy.c: In function ‘main’:
snappy.c:24:12: error: variable ‘snappy’ has initializer but incomplete type
 struct fish snappy = {"Snappy", "piranha", 69, 4};
        ^
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:17: error: storage size of ‘snappy’ isn’t known
 struct fish snappy = {"Snappy", "piranha", 69, 4};

最佳答案

struct Fish = {struct 声明中是错误的。它应该是structfish{。删除 = 符号。

关于c - 大量结构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065389/

相关文章:

c - 使用 Frama-C 进行值(value)依赖分析

c++ - C++ 中结构的构造函数导致非零退出代码

c++ - 结构中的枚举

C 结构编译错误

c - 用 C 提取二进制文件的最后 128 个字节

c++ - Cudd:提取变量排序

c - 读取字符串并在结构中存储为 int 的最安全方法

c - 基本生成文件程序 : in main Error undefined reference to "power"

c - 运行 C 代码时输出错误

go - 如何从数组创建Golang结构实例?