Re: legality of forward declaration
- From: "Harald van Dijk" <truedfx@xxxxxxxxx>
- Date: 31 May 2006 08:13:19 -0700
Vladimir Oka wrote:
Kenneth Brody wrote:
Vladimir Oka wrote:
[...]
Martin Eisenberg wrote:
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)?
Current versions of gcc will reject it as well.
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).
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?
When you are able to declare a parameter as either an array type or as
a pointer type, there is absolutely no difference in its meaning.
However, that does not mean you will always be able to declare a
parameter as an array type, just because you can declare it as a
pointer type.
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.
Hmmm. I may have messed this one up.
No, I think you were right the first time.
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.
Any array of incomplete (or function) type is a constraint violation
(6.7.5.2), and so a diagnostic is required. There is no exception for
array types for function parameters.
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...
.
- References:
- Re: legality of forward declaration
- From: Vladimir Oka
- Re: legality of forward declaration
- From: Kenneth Brody
- Re: legality of forward declaration
- From: Vladimir Oka
- Re: legality of forward declaration
- Prev by Date: Re: Bit fiddling
- Next by Date: Re: Simplicity has a future
- Previous by thread: Re: legality of forward declaration
- Next by thread: Re: legality of forward declaration
- Index(es):
Relevant Pages
|