Is the order of prototype declaration and structure declaration in the following program correct?

I saw this question from a book

Is the order of prototype declaration and structure declaration in the following program correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #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:
1
2
struct emp;
void flu(emp*);

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.

i.e. this will give error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <stdio.h>

struct emp{
	char name[20];
	int age;
};

int main()
{
	struct emp e = { "soicher", 34 };
	fiu(e);
	return 0;
}

void fiu(struct emp ee)
{
	printf("%s %d\n", ee.name, ee.age);
}


But this wont because the compiler knows about the function before the main calls it, thats why we declare function prototypes at the top.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <stdio.h>

struct emp{
	char name[20];
	int age;
};

void fiu(struct emp ee)
{
	printf("%s %d\n", ee.name, ee.age);
}

int main()
{
	struct emp e = { "soicher", 34 };
	fiu(e);
	return 0;
}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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);
}

http://coliru.stacked-crooked.com/a/0d65047ff57e04a2
Topic archived. No new replies allowed.