Re: legality of forward declaration




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.

Code Warrior (version 3.2.5), though, produces the following
error message:

------> CW problem starts here <------
C:\temp>mwccsym2.exe -g -O0 -inline
off -wchar_t off -align 4 -warnings on -w
nohidevirtual,nounusedexpr -msgstyle
gcc -enum int -str pool -exc ms -trigraphs on -stdinc -d _DEBUG
-d _UNICODE -d "
__SYMBIAN32__" -d "__SERIES60_30__" -d "__SERIES60_3X__" -d
"__CW32__" -d "__WIN
S__" -d "__WINSCW__" -d "WIN32" -d "_WINDOWS" -d
"__SUPPORT_CPP_EXCEPTIONS__" -c
wd source -i- -o forwardDeclaration.o -c forwardDeclaration.c
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).

PS
Posting full compilable example is, as always, the best policy when
asking this sort of question.

.



Relevant Pages

  • Re: How to pass a struct to a function
    ... write an expression evaluating to your struct -- the name of a variable ... you haven't declared a type "struct entry". ... about how this declaration is handled, ... on which compiler you're using, but any decent compiler will print ...
    (comp.lang.c)
  • Re: void * vs char *
    ... cast p to a (struct s*) to avoid a compile-time error. ... struct s {int a;}; ... function call consists solely of an identifier, and if no declaration ... And see what your compiler has to say. ...
    (comp.lang.c)
  • Re: Share .cpp and .h along projects
    ... You asked me to show how a volatile pointer could be used to ... volatile void* or a cast could be used). ... As you stated "A compiler that can see into all these functions will ...
    (microsoft.public.vc.language)
  • Re: Passing an already declarted array of structures!
    ... void LCD_PAINTSCREEN ... in above code by "int", ... If you guys see nothing wrong with the code, then I suppose its a compiler ... struct S { ...
    (microsoft.public.vc.language)
  • Re: legality of forward declaration
    ... struct ForwardDeclared; ... void foo; ... declaration of your `struct`. ... When you are able to declare a parameter as either an array type or as ...
    (comp.lang.c)