an error about C struct array in formal parameter

  • A+

I have got the following code:

struct student_info; void paiming1(struct student_info student[]);  struct student_info   {     int num;      char name[6];  }; 

The IDE gives an error

error: array type has incomplete element type ‘struct student_info’  void paiming1(struct student_info student[]); 

But if I use void paiming1(struct student_info *student); it works OK. Why is that? I am using GCC.

С language unconditionally requires array element type in all array declarations to be complete. Period. Array declarators
1 [...] The element type shall not be an incomplete or function type. [...]

No exception is made for array declarations used in function parameter lists. This is different from C++ - the latter drops this completeness requirement for function parameter lists

struct S; void foo(struct S[]); // OK in C++, invalid in C 

Considering that in parameter list declaration type T [] is later adjusted to type T * anyway, this requirement might appear to be excessive. (And this is why C++ relaxed it.) Nevertheless, this restriction is present in C language. This is just one of the quirks of C language.

As you already know, you can explicitly switch to the equivalent

void paiming1(struct student_info *student);  

form to work around the issue.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: