(a) First, let’s take a look at an example where the definition and declaration of a function are inconsistent:
#include <stdio.h> int func(int a); int func(void) { return 0; } int main(void) { func(); return 0; }
Compiler:
gcc -g -o a a.c a.c:5:5: error: conflicting types for ‘func’ int func(void) { ^ a.c:3:5: note: previous declaration of ‘func’ was here int func(int a);
You can see that this error occurs at compile time because the declaration and definition of “func” are inconsistent (one with arguments and one without).
(b) Recently, when I ported some code to JFFS2, this error also occurred at compile time. But I found that the declaration and definition of the function in the header file were exactly the same, which surprised me. The conclusion is that the function parameter type is defined after the function is declared. The simplified code is as follows:
#include <stdio.h> void func(struct A *A); struct A { int a; }; void func(struct A *A) { printf("%d", A->a); } int main(void) { // your code goes here struct A a = {4}; func(&a); return 0; }
Where the definition of “structure A” is placed after the “func” function declaration, and the func function argument is the “structure A*” type. The compilation results are as follows:
gcc -g -o a a.c a.c:3:18: warning: ‘struct A’ declared inside parameter list void func(struct A *A); ^ a.c:3:18: warning: its scope is only this definition or declaration, which is probably not what you want a.c:9:6: error: conflicting types for ‘func’ void func(struct A *A) ^ a.c:3:6: note: previous declaration of ‘func’ was here void func(struct A *A); ^
You can also see the output error “error: Conflicting Types for ‘func'”. Perhaps a compilation warning is a clue.