Re: mutually referential (Pg 140 K&R2)



On 22 Apr 2005 21:13:49 -0700, "G Patel" <gaya.patel@xxxxxxxxx> wrote:

>Pg. 140 of K&R2 shows an example of mutually referential structure
>declarations...
>
>struct t
>{
> struct s *p;
>};
>
>struct s
>{
> struct t *q;
>};
>
>
>I ran the following test program through my C compiler to see if it
>would compile:
>
>#include <stdlib.h>
>
>struct t
>{
> struct s *p; /* type 'struct s' is not visible here */
>};
>
>struct s
>{
> struct t *q;
>};
>
>int main()
>{
> printf("Hello world\n");
> return 0;
>}
>
>
>It compiled and ran without any noticeable problems. I expected it to
>complain about the line I've commented above.
>
>How is this valid when the type 'struct s' is not in scope when *p is
>declared as a member of the type struct t?
>
>If this is 'valid' please tell me why scopes don't apply here. If this
>is 'not valid' please tell me how I can declare mutually referential
>struct types.
>

The fact that you compiler does not generate a diagnostic has no
bearing on whether the code is valid or not. The opposite is also
true. The compiler is allowed to generate a diagnostic even if the
code is perfectly valid.

Maybe you should raise the warning level on your compiler.

Maybe your compiler is defective in this area.

Maybe it is an extension your compiler provides. Can you disable
extensions?


<<Remove the del for email>>
.



Relevant Pages

  • Re: komplexes Problem mit Funktionszeigern
    ... Die Funktion gibt einen struct "by value" zurueck und bei der ... am Borland Compiler, da der Microsoft Compiler ... Man kann Argumente fuer eine Funktion z.B auf dem Stack uebergeben. ... Das funktioniert natuerlich nur fuer "Basis Typen" die in ein Register passen. ...
    (de.comp.lang.c)
  • Re: Naming typedefs
    ... problem that a scalar could be returned in the A register ... to have waste precious cpu cycles copying the struct there ... of the hardware or twentieth century compiler technology. ... # to include the header defining it into every other header (assuming ...
    (comp.unix.programmer)
  • Re: assembly language and reverse engineering
    ... "return arguments to struct pointed to by register" and ... assumptions of CDECL-like conventions (also supports STDCALL, ... arg for handling struct return (slightly compiler, and compiler version, ...
    (alt.lang.asm)
  • Re: HardBound and SoftBound (was "The State of Software")
    ... I will explain to you one last time that the problem is not whether a compiler can generate checks for all buffer overflows. ... By the way, in the last example of such code that I saw, it had been changed to use the address of the struct, not of the first element of the struct: ... to take address of struct rather than address of first element. ... such tricks are used to create a pointer which is then passed on. ...
    (comp.arch)
  • Re: List<> of struct with property. Cannot change value of property. why?
    ... method changes the struct that owns it. ... how would the compiler know the method changes the struct? ... Should the linker be required to carry this flag around too, so that when you import a reference to a struct type not compiled with the current project, you still have that information? ... There's a time and place for a value type, and in fact they even have their place in lists. ...
    (microsoft.public.dotnet.languages.csharp)