前瞻性声明的合法性 [英] legality of forward declaration

查看:68
本文介绍了前瞻性声明的合法性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨Antoine,只是重定向你...


Antoine Trux写道:

Hi Antoine, just redirecting you...

Antoine Trux wrote:



以下代码是否合法:

------>代码从这里开始< ------
#include< stddef.h>
struct ForwardDeclared;
void foo(struct ForwardDeclared []);
/ * Dummy函数,所以这个模块包含一些代码。 * /
void dummy(void)
{
foo(NULL);
}
------>代码在这里结束< ------


我看不出为什么这段代码不合法​​,而且确实
gcc(版本3.4.5)和Microsoft C编译器(.NET)编译它没有故障。

Code Warrior(版本3.2.5),产生以下
错误消息:

------> CW问题从这里开始< ------
C:\ temp> mwccsym2.exe -g -O0 -inline
off -wchar_t off -align 4 -warnings on -w nohidevirtual,nounusedexpr -msgstyle
gcc -enum int -str pool -exc ms -trigraphs on -stdinc -d _DEBUG
-d _UNICODE -d"
__SYMBIAN32 __" -d__ SERIES60_30 __ -d__ SERIES60_3X __ -d
" __ CW32 __" -d__ WIN
S __ -d__ WINSCW __ -dWIN32 -d_WINDOWS -d
" __ SUPPORT_CPP_EXCEPTIONS __" -c
wd source -i--forward forwardDeclaration.o -c forwardDeclaration.c
forwardDeclaration.c:5:非法使用不完整的
struct / union / class''struct for
wardDeclared''
错误导致工具中止。
------> CW问题在此结束< ------

改变这一行:
void foo(struct ForwardDeclared []);
to:
void foo( struct ForwardDeclared *);
然而,纠正了问题。

所以,这是(我相信)Code Warrior中的一个错误(错误地抱怨正确的代码) ),或者gcc和MS编译器中的错误
(不检测出错误的代码)?

Antoine Trux
Hi,

Is the following code legal:

------> code starts here <------
#include <stddef.h>
struct ForwardDeclared;
void foo(struct ForwardDeclared[]);
/* Dummy function, so this module contains some code. */
void dummy(void)
{
foo(NULL);
}
------> code ends here <------
?

I can see no reason why this code would not be legal, and indeed
gcc (version 3.4.5) and Microsoft C compiler (.NET) compile it
without a glitch.

Code Warrior (version 3.2.5), though, produces the following
error message:

------> CW problem starts here <------
C:\temp>mwccsym2.exe -g -O0 -inline
off -wchar_t off -align 4 -warnings on -w
nohidevirtual,nounusedexpr -msgstyle
gcc -enum int -str pool -exc ms -trigraphs on -stdinc -d _DEBUG
-d _UNICODE -d "
__SYMBIAN32__" -d "__SERIES60_30__" -d "__SERIES60_3X__" -d
"__CW32__" -d "__WIN
S__" -d "__WINSCW__" -d "WIN32" -d "_WINDOWS" -d
"__SUPPORT_CPP_EXCEPTIONS__" -c
wd source -i- -o forwardDeclaration.o -c forwardDeclaration.c
forwardDeclaration.c:5: illegal use of incomplete
struct/union/class ''struct For
wardDeclared''
Errors caused tool to abort.
------> CW problem ends here <------

Changing this line:
void foo(struct ForwardDeclared[]);
to:
void foo(struct ForwardDeclared*);
however, corrects the "problem".

So, is this (as I believe) a bug in Code Warrior (incorrectly
complains about correct code), or a bug in gcc and MS compilers
(do not detect code that is in error)?

Antoine Trux


推荐答案



Martin Eisenberg写道:

Martin Eisenberg wrote:
你好Antoine,只是重定向你......

Antoine Trux写道:
Hi Antoine, just redirecting you...

Antoine Trux wrote:


以下代码是否合法:

------>代码从这里开始< ------
#include< stddef.h>
struct ForwardDeclared;
void foo(struct ForwardDeclared []);
/ * Dummy函数,所以这个模块包含一些代码。 * /
void dummy(void)
{
foo(NULL);
}
------>代码在这里结束< ------


我看不出为什么这段代码不合法​​,而且确实
gcc(版本3.4.5)和Microsoft C编译器(.NET)编译它没有故障。

Code Warrior(版本3.2.5),产生以下
错误消息:

------> CW问题从这里开始< ------
C:\ temp> mwccsym2.exe -g -O0 -inline
off -wchar_t off -align 4 -warnings on -w nohidevirtual,nounusedexpr -msgstyle
gcc -enum int -str pool -exc ms -trigraphs on -stdinc -d _DEBUG
-d _UNICODE -d"
__SYMBIAN32 __" -d__ SERIES60_30 __ -d__ SERIES60_3X __ -d
" __ CW32 __" -d__ WIN
S __ -d__ WINSCW __ -dWIN32 -d_WINDOWS -d
" __ SUPPORT_CPP_EXCEPTIONS __" -c
wd source -i--forward forwardDeclaration.o -c forwardDeclaration.c
forwardDeclaration.c:5:非法使用不完整的
struct / union / class''struct for
wardDeclared''
错误导致工具中止。
------> CW问题在此结束< ------

改变这一行:
void foo(struct ForwardDeclared []);
to:
void foo( struct ForwardDeclared *);
然而,纠正了问题。

所以,这是(我相信)Code Warrior中的一个错误(错误地抱怨正确的代码) ),或者gcc和MS编译器中的错误
(不检测出错误的代码)?
Hi,

Is the following code legal:

------> code starts here <------
#include <stddef.h>
struct ForwardDeclared;
void foo(struct ForwardDeclared[]);
/* Dummy function, so this module contains some code. */
void dummy(void)
{
foo(NULL);
}
------> code ends here <------
?

I can see no reason why this code would not be legal, and indeed
gcc (version 3.4.5) and Microsoft C compiler (.NET) compile it
without a glitch.

Code Warrior (version 3.2.5), though, produces the following
error message:

------> CW problem starts here <------
C:\temp>mwccsym2.exe -g -O0 -inline
off -wchar_t off -align 4 -warnings on -w
nohidevirtual,nounusedexpr -msgstyle
gcc -enum int -str pool -exc ms -trigraphs on -stdinc -d _DEBUG
-d _UNICODE -d "
__SYMBIAN32__" -d "__SERIES60_30__" -d "__SERIES60_3X__" -d
"__CW32__" -d "__WIN
S__" -d "__WINSCW__" -d "WIN32" -d "_WINDOWS" -d
"__SUPPORT_CPP_EXCEPTIONS__" -c
wd source -i- -o forwardDeclaration.o -c forwardDeclaration.c
forwardDeclaration.c:5: illegal use of incomplete
struct/union/class ''struct For
wardDeclared''
Errors caused tool to abort.
------> CW problem ends here <------

Changing this line:
void foo(struct ForwardDeclared[]);
to:
void foo(struct ForwardDeclared*);
however, corrects the "problem".

So, is this (as I believe) a bug in Code Warrior (incorrectly
complains about correct code), or a bug in gcc and MS compilers
(do not detect code that is in error)?




好​​吧,如果代码上面是编译单元中的* only *代码,

那么我担心CodeWarrior是对的。你需要的是你的`struct`的正确的

声明。也许你错过了包含声明的`#include`

?数组和指针不是同一个东西

在C中(尽管有些消息来源说)。


PS

发布完全可编辑例如,

提出这类问题的最佳政策。



Well, if the code above is the *only* code in your compilation unit,
then I''m afraid the CodeWarrior is right. What you need is a proper
declaration of your `struct`. Perhaps you''re missing an `#include`
containing the declaration? Arrays and pointers are not the same thing
in C (despite what some sources say).

PS
Posting full compilable example is, as always, the best policy when
asking this sort of question.


Vladimir Oka写道:
Vladimir Oka wrote:

Martin Eisenberg写道:

Martin Eisenberg wrote:
嗨Antoine,只是重定向你......

Antoine Trux写道:
Hi Antoine, just redirecting you...

Antoine Trux wrote:


以下代码是否合法:

------>代码从这里开始< ------
#include< stddef.h>
struct ForwardDeclared;
void foo(struct ForwardDeclared []);
[...] forwardDeclaration.c:5:非法使用不完整的
struct / union / class''struct for
wardDeclared''
错误导致工具中止。
------> CW问题在此结束< ------

改变这一行:
void foo(struct ForwardDeclared []);
to:
void foo( struct ForwardDeclared *);
然而,纠正了问题。

所以,这是(我相信)Code Warrior中的一个错误(错误地抱怨正确的代码) ),或者gcc和MS编译器中的错误
(不检测出错误的代码)?
Hi,

Is the following code legal:

------> code starts here <------
#include <stddef.h>
struct ForwardDeclared;
void foo(struct ForwardDeclared[]); [...] forwardDeclaration.c:5: illegal use of incomplete
struct/union/class ''struct For
wardDeclared''
Errors caused tool to abort.
------> CW problem ends here <------

Changing this line:
void foo(struct ForwardDeclared[]);
to:
void foo(struct ForwardDeclared*);
however, corrects the "problem".

So, is this (as I believe) a bug in Code Warrior (incorrectly
complains about correct code), or a bug in gcc and MS compilers
(do not detect code that is in error)?


好吧,如果上面的代码是编译单元中的* only *代码,
然后我担心CodeWarrior是对的。你需要的是你的`struct`的正确声明。也许你错过了包含声明的#include
?数组中的数组和指针不是一样的(尽管有些消息来源说过)。


Well, if the code above is the *only* code in your compilation unit,
then I''m afraid the CodeWarrior is right. What you need is a proper
declaration of your `struct`. Perhaps you''re missing an `#include`
containing the declaration? Arrays and pointers are not the same thing
in C (despite what some sources say).




但是,给定一个函数原型:


void foo(struct ForwardDeclared []);



void foo(struct ForwardDeclared *);


是一回事,我不明白为什么CodeWarrior会抱怨。你可以指向不完整类型的指针,只要你不尝试使用

除了在其他地方传递之外的任何东西。

或者声明为

数组的函数参数与指针之间是否有区别?

PS
发布完全可编译的示例是,as总是,这是提出这类问题时的最佳政策。



But, given that a function prototype of:

void foo(struct ForwardDeclared[]);
and
void foo(struct ForwardDeclared *);

are the same thing, I don''t see why CodeWarrior is complaining. You
can have pointers to incomplete types as long as you don''t try to use
them for anything other than passing along elsewhere.

Or is there a difference between a function parameter declared as an
array versus a pointer?
PS
Posting full compilable example is, as always, the best policy when
asking this sort of question.




这看起来像是一个可编辑的例子。即使警告上升到最大值,它在我的系统上编译也很好。

-

+ ------ ------------------- + -------------------- + --------- -------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>



It looks like a compilable example to me. It compiles just fine on
my system, even with warnings turned up to the max.
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>



Kenneth Brody写道:

Kenneth Brody wrote:
Vladimir Oka写道:
Vladimir Oka wrote:

Martin Eisenberg写道:

Martin Eisenberg wrote:
嗨Antoine,只是重定向你...

Antoine Trux写道:

>
>
>以下代码是否合法:
>
> ------>代码从这里开始< ------
> #include< stddef.h>
> struct ForwardDeclared;
> void foo(struct ForwardDeclared []); [...]> forwardDeclaration.c:5:非法使用不完整
> struct / union / class''struct for
> wardDeclared''
>错误导致工具中止。
> ------> CW问题在此结束< ------
>
>改变这一行:
> void foo(struct ForwardDeclared []);
>致:
> void foo(struct ForwardDeclared *);
>但是,纠正了问题。
>
>那么,这是(我相信)Code Warrior中的错误(错误地
>抱怨正确的代码),或gcc和MS编译器中的错误
> (不检测出错的代码)?
Hi Antoine, just redirecting you...

Antoine Trux wrote:

> Hi,
>
> Is the following code legal:
>
> ------> code starts here <------
> #include <stddef.h>
> struct ForwardDeclared;
> void foo(struct ForwardDeclared[]); [...] > forwardDeclaration.c:5: illegal use of incomplete
> struct/union/class ''struct For
> wardDeclared''
> Errors caused tool to abort.
> ------> CW problem ends here <------
>
> Changing this line:
> void foo(struct ForwardDeclared[]);
> to:
> void foo(struct ForwardDeclared*);
> however, corrects the "problem".
>
> So, is this (as I believe) a bug in Code Warrior (incorrectly
> complains about correct code), or a bug in gcc and MS compilers
> (do not detect code that is in error)?



好吧,如果上面的代码是编译单元中的* only *代码,那么我很害怕CodeWarrior是对的。你需要的是你的`struct`的正确声明。也许你错过了包含声明的#include
? C中的数组和指针不是一回事(尽管有些消息来源说)。



Well, if the code above is the *only* code in your compilation unit,
then I''m afraid the CodeWarrior is right. What you need is a proper
declaration of your `struct`. Perhaps you''re missing an `#include`
containing the declaration? Arrays and pointers are not the same thing
in C (despite what some sources say).



但是,鉴于以下函数原型:
void foo(struct ForwardDeclared []);

void foo(struct ForwardDeclared *);

是一回事,我不明白为什么CodeWarrior会抱怨。你可以指出不完整类型的指针,只要你不尝试将它们用于除了在其他地方传递之外的任何东西。

或者是否有区别函数参数声明为
数组与指针?



But, given that a function prototype of:

void foo(struct ForwardDeclared[]);
and
void foo(struct ForwardDeclared *);

are the same thing, I don''t see why CodeWarrior is complaining. You
can have pointers to incomplete types as long as you don''t try to use
them for anything other than passing along elsewhere.

Or is there a difference between a function parameter declared as an
array versus a pointer?

PS
发布完整的可编辑示例一如既往是最好的政策时
问这个有点问题。
PS
Posting full compilable example is, as always, the best policy when
asking this sort of question.



它看起来像是一个可编辑的例子。它在我的系统上编译得很好,即使警告已经达到最大值。



It looks like a compilable example to me. It compiles just fine on
my system, even with warnings turned up to the max.




嗯。我可能搞砸了这个。


唯一*警告*我得到的是:


main.c:3:警告:数组类型有不完整的元素类型


我不认为标准是必需的。


我认为你是对的,我错了。


不幸的是,我现在没有时间深入挖掘它

(火车时刻表不在乎关于C),但我相信有人会。


PS

我仍​​然认为参数声明应该使用指针而不是
$ b但是,$ b的数组。我知道,这是一种风格问题,但是......



Hmmm. I may have messed this one up.

The only *warning* I get is:

main.c:3: warning: array type has incomplete element type

Which I don''t think is required by the standard.

I think you''re right, and I was wrong.

Unfortunately, I don''t have time at the moment to dig deeper into it
(train timetables don''t care about C), but I''m sure someone will.

PS
I still think parameter declaration should have used pointers instead
of arrays, though. A matter of style, I know, but...


这篇关于前瞻性声明的合法性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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