Re: legality of forward declaration



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
.



Relevant Pages

  • Re: [9fans] A Plan 9 C request....
    ... Does that mean the following will compile? ... t.c:6: error: 'for' loop initial declaration used outside C99 mode ... compiler steps out of C99 mode and refuses to allow the C99 construct ...
    (comp.os.plan9)
  • Re: menu-based console application
    ... typedef void; ... struct MenuEntry ... Compile everything, fix typing errors and run the program. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: CFile and FILE*
    ... ever call exit() in any C++ ... LPVOID, e.g., ... void DoSomething ... Note that you can still compile this as C ...
    (microsoft.public.vc.mfc)
  • Re: A non-const reference may only be bound to an lvalue?
    ... To my surprise, I have tested that the following code can compile, ... struct X { ... void f; ... There are legitimate reasons to want to do that, but I've come to accept it ...
    (microsoft.public.vc.language)
  • Re: Library bug or my fault?
    ... void memcpy ... struct Foo { ... 29 void cp(const Foo *foo) ... You have no definition for cp2so the code above won't compile. ...
    (comp.lang.c)