多种结构错误

时间:2015-03-15 19:48:50

标签: 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};

1 个答案:

答案 0 :(得分:1)

struct fish = {声明中的

struct错误。它应该是struct fish {。删除=符号。