【问题标题】:i have an error:"incomplete definition of type "struct Bird""我有一个错误:““struct Bird”类型的定义不完整”
【发布时间】:2021-05-16 11:58:37
【问题描述】:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <sys/io.h>
#include <fcntl.h>
    
int main(){
  struct BirdHome{
    int area;
    int height;
    int feederquantity;
    char hasNest[1];
  };
  struct Bird{
    char isRinged[1];
    char nameSpecies[50];
    int birdAgeMonths;
    struct BirdHome hom;
    char gender[1];
  };

  struct Bird sparrow;
  strcpy(sparrow.isRinged,"T");
  strcpy(sparrow.nameSpecies,"sparrow");
  sparrow.birdAgeMonths=4;
  sparrow.hom.area=30;
  sparrow.hom.height=20;
  sparrow.hom.feederquantity=2;
  strcpy(sparrow.hom.hasNest,"F");
  strcpy(sparrow.gender,"M");
  printBird(&sparrow);
  return 0;
}



void printBird(struct Bird *bird){
  printf("Bird info:\n");
  printf("\tIs bird ringed:%s\n", (*bird).isRinged);
  printf("\tThe name of the specie:\n",(*bird).nameSpecies);
  printf("\tThe birds age is:%d\n",(*bird).birdAgeMonths );
  printf("Bird house info:\n");
  printf("\tArea of its house is:%d\n",(*bird).hom.area);
  printf("\tThe height of its house is:%d\n",(*bird).hom.height);
  printf("\tThe quantity of feeders is:%d\n",(*bird).hom.feederquantity );
  printf("\tThe house has nest:%s\n",(*bird).hom.hasNest );
  printf("\tBird's gender is:%s\n",(*bird).gender );
}

我尝试添加任何可能的库(您可以在代码中看到它),但编译代码时仍然会发生错误。好吧,如果我们在主函数中重新编码该函数(使用所有已编辑的代码),它可以正常工作,但我希望它与一个单独的函数一起工作。你能帮我吗?

【问题讨论】:

  • 如果 Bird.isRinged 的长度为 1,则此 strcpy(sparrow.isRinged,"T"); 会导致未定义的行为。请记住,strcpy 在字符串中的所有字符之后复制一个空字符。我怀疑您真的只想将其设为单个 char 而不是字符串。或者使用布尔值。
  • 您必须在函数main 之外定义结构才能在mainprintBird 中使用它。顺便说一句:至少对于问题中显示的代码,您有几个不需要的 #include 指令。

标签: c struct scope declaration string-literals


【解决方案1】:

struct Bird 类型的作用域是函数 main 的外部块作用域。

int main(){
  struct BirdHome{
    int area;
    int height;
    int feederquantity;
    char hasNest[1];
  };
  struct Bird{
    char isRinged[1];
    char nameSpecies[50];
    int birdAgeMonths;
    struct BirdHome hom;
    char gender[1];
  };

  //...

所以在 main 函数之外,类型 struct Bird 是未声明和未定义的。

然而,在函数 printBird 的定义中,应该在函数 main 之前声明,您正在尝试使用结构的数据成员,尽管在此范围内,在 main 中声明的 struct Bird 未声明且未定义。

void printBird(struct Bird *bird){
  printf("Bird info:\n");
  printf("\tIs bird ringed:%s\n", (*bird).isRinged);
  //..

即在函数参数列表中你声明了不完整类型struct Bird

void printBird(struct Bird *bird){
               ^^^^^^^^^^^

这与 main 中声明的 struct Bird 没有任何共同之处。

main 移动结构定义,使其定义可用于函数printBird。然后将函数声明放在函数main之前。

由于结构类型的对象通过引用传递给函数并且在其中没有被更改,那么你应该使用 quakifier const

void printBird( const struct Bird *bird);

注意这个调用

strcpy(sparrow.isRinged,"T");

调用未定义的行为,因为数组 isRinged 不足以存储字符串文字“T”,该字符串在内部表示为具有两个元素 { 'T', '\0' } 的字符数组。

你至少应该像这样声明它

char isRinged[2];

这些语句也存在同样的问题

strcpy(sparrow.hom.hasNest,"F");
strcpy(sparrow.gender,"M");

你需要放大使用的字符数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    相关资源
    最近更新 更多