Re: legality of forward declaration
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 31 May 2006 15:15:56 +0100
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[]);
/* 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.
Try harder with gcc and you might get it to generate a warning.
markg@markgordon-lp ~
$ cat t.c
#include <stddef.h>
struct ForwardDeclared;
void foo(struct ForwardDeclared[]);
/* Dummy function, so this module contains some code. */
void dummy(void)
{
foo(NULL);
}
markg@markgordon-lp ~
$ gcc -c t.c -ansi -pedantic
t.c:3: warning: array type has incomplete element type
Code Warrior (version 3.2.5), though, produces the following
error message:
<snip>
Changing this line:
void foo(struct ForwardDeclared[]);
to:
void foo(struct ForwardDeclared*);
however, corrects the "problem".
This is a sensible change if it is meant to be a pointer to an incomplete type. If, however, the definition of the struct is meant to be visible in the file, making it visible before this function declaration would be sensible.
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).
Yes. However, in every other respect in a function declaration
void foo(struct ForwardDeclared[]);
and
void foo(struct ForwardDeclared*);
are equivalent since both specify that foo takes a pointer to struct ForwardDeclared as a parameter.
PS
Posting full compilable example is, as always, the best policy when
asking this sort of question.
Not when the problem is that it won't compile as in this case! That's a bit like saying to someone they should not go to the doctor for advice until they have recovered from whatever they are suffering from!
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
.
- References:
- Re: legality of forward declaration
- From: Vladimir Oka
- Re: legality of forward declaration
- Prev by Date: Re: legality of forward declaration
- Next by Date: Re: malloc() implementation
- Previous by thread: Re: legality of forward declaration
- Index(es):
Relevant Pages
|