#include <stdio.h>
void fiu(struct emp);
struct emp{
char name[20];
int age;
};
int main()
{
struct emp e = {"soicher",34};
fiu(e);//error here-- type of formal parameter 1 is incomplete
}
void fiu (struct emp ee)//error here-- conflicting types for fiu
{
printf("%s %d\n",ee.name,ee.age);
}
the book said yes,the order of declarations does not matter while the compiler is giving me error. However if i change the order of declaration the error disappears. I don't understand the error. The errors have been described in comments in code.
To generate function prototype, compiler should know full definition of all types passed by value at the moment of declaration. He does not know anything about struct emp: nothing about size, not even about it existence at all!
If you are using C++ or modern C, you do not even need to write struct emp in types of arguments and variables (lines 3, 14 and 18)
If you would take argument by pointer, forward declaration ("promise" of future definition) would suffice:
You should declare the structure first, since the parameter of fiu uses the structure and would cause issues.
For example, take away line 3 and run your program, you will get an error because your main function attempts to call a function it is unaware of, i.e. below it - then move the function above main then try it again and it will work.
the book said yes,the order of declarations does not matter while the compiler is giving me error
The order of "Function" declarations doesn't matter but if those functions have structure parameters like yours then the compiler should know about the structure first.
#include <stdio.h>
struct emp ; // declare emp // *** added ***
void fiu(struct emp); // declare function which takes an emp by value
struct emp{ // define emp
char name[20];
int age;
};
int main()
{
struct emp e = {"soicher",34};
fiu(e); // *** fine ***
}
void fiu (struct emp ee) // *** also fine *** define function which takes an emp by value
{
printf("%s %d\n",ee.name,ee.age);
}