mutually referential (Pg 140 K&R2)



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.

Thank you

.



Relevant Pages

  • Re: null function pointer?
    ... >>> declarations have the same scope and use the same struct tag). ... >>> `f' is a pointer to incomplete type. ... > the type specifiers mean the same type at all points (well, ...
    (comp.lang.c)
  • Re: null function pointer?
    ... >>> The type is incomplete until the closing brace of the list ... >> point of the code in your example in all declarations `struct foo' ... >> declarations have the same scope and use the same struct tag). ... the type specifiers mean the same type at all points (well, ...
    (comp.lang.c)
  • Re: how to define a function pointer variable witout typdef?
    ... >> But then why other declarations are not definitions? ... and that a typedef actually ... # identifier can denote different entities at different points ... struct, union, enum members ...
    (comp.lang.c)
  • Re: header file
    ... functions declarations and the basic struct definition. ... In my opinion gcc file1.c file2.c should do the linking. ...
    (comp.lang.c)
  • Re: [PATCH] Include missing header in include/asm-sparc64/tlb.h
    ... It builds perfectly fine for me with several different config variants ... MM layer structure definitions or forward declarations related ... dereferences a struct page. ... Perhaps you have a local change of some kind that creates this ...
    (Linux-Kernel)