#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 );
}
I've tried to add any libraries possible(you can see it in the code), but the error still happens when compiling code. well, if we recode the function in the main one(with all the redacted code), it works normally, but i want it to wprk with a separate one. can you help me with it?
The scope of the type struct Bird is the outer block scope of the function 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];
};
//...
So outside the function main the type struct Bird
is undeclared and undefined.
However in the definition of the function printBird
that should be declared before the function main
you are trying to use data members of the structure though in this scope the struct Bird
declared in main is undeclared and undefined.
void printBird(struct Bird *bird){
printf("Bird info:\n");
printf("\tIs bird ringed:%s\n", (*bird).isRinged);
//..
That is in the function parameter list you declared the incomplete type struct Bird
void printBird(struct Bird *bird){
^^^^^^^^^^^
that has nothing common with the struct Bird
declared in main.
Move the structure definition from main
such a way that its definition would be available for the function printBird
. And after that place the function declaration before the function main
.
Also as the object of the structure type is passed by reference to the function and is not being changed within it then you should use the quakifier const
void printBird( const struct Bird *bird);
Pay attention to that this call
strcpy(sparrow.isRinged,"T");
invokes undefined behavior because the array isRinged
is not large enough to store the string literal "T" that internally is represented as a character array with two elements { 'T', '\0' }
.
You should declare it at least like
char isRinged[2];
The same problem exists with these statements
strcpy(sparrow.hom.hasNest,"F");
strcpy(sparrow.gender,"M");
You need to enlarge the used character arrays.