Re: legality of forward declaration
- From: "Vladimir Oka" <novine@xxxxxxxxxxxxxxx>
- Date: 31 May 2006 07:23:47 -0700
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)?
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?
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.
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...
.
- Follow-Ups:
- Re: legality of forward declaration
- From: Harald van Dijk
- Re: legality of forward declaration
- References:
- Re: legality of forward declaration
- From: Vladimir Oka
- Re: legality of forward declaration
- From: Kenneth Brody
- Re: legality of forward declaration
- Prev by Date: Re: malloc() implementation
- 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
|