Re: legality of forward declaration




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...

.



Relevant Pages

  • Re: void *
    ... struct list_node *next; ... It would probably help if your structure had a member which identified ... That was my understanding of 'void *' concept. ... it guarantees that you can supply pointers to the various data ...
    (comp.lang.c)
  • Re: generic linklist
    ... you can create a list structure that's type-agnostic by using void ... pointers for the list node data and by associating type-aware ... struct glnode *next; ... int cmpStrings (const void *lhs, ...
    (comp.lang.c)
  • Re: Is this legal
    ... This is a cut down example of some code I've written to iterate ... typedef void list_function; ... struct generic_list *next; ... between pointers to void and pointers to struct appear valid. ...
    (comp.lang.c)
  • Re: legality of forward declaration
    ... struct ForwardDeclared; ... void foo; ... Errors caused tool to abort. ... or a bug in gcc and MS compilers ...
    (comp.lang.c)
  • Re: Typedef question
    ... easier to write and read `HashItem' than `struct hashItem', ... pointers to private structs, the fact that they're pointers is usually ... prefer to deal with known-to-be-pointers to opaque data. ... void DestroyMyADT; ...
    (comp.lang.c)