形式参数中有关C struct数组的错误 [英] an error about C struct array in formal parameter

查看:91
本文介绍了形式参数中有关C struct数组的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

IDE出现错误

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

但是,如果我使用void paiming1(struct student_info *student);,它可以正常工作.这是为什么?我正在使用GCC.

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.

6.7.6.2数组声明符
约束
1 [...]元素类型不得为不完整或函数 类型. [...]

6.7.6.2 Array declarators
Constraints
1 [...] The element type shall not be an incomplete or function type. [...]

在函数参数列表中使用的数组声明没有例外.这与C ++不同-C ++放弃了对函数参数列表的完整性要求

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

考虑到以后在参数列表声明中将类型T []调整为类型T *,此要求可能看起来过高. (这就是C ++放松它的原因.)但是,此限制在C语言中存在.这只是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); 

表格以解决此问题.

这篇关于形式参数中有关C struct数组的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆