Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: ANSI-C Data...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 1054 of 1133
Post > Topic >>

Re: ANSI-C Data Type "Prototyping" & Circular-Referenced Definitions ...

by Jack Klein <jackklein@[EMAIL PROTECTED] > Mar 17, 2008 at 03:20 PM

On Sat, 8 Mar 2008 12:21:09 -0600 (CST), ericshufro@[EMAIL PROTECTED]
 wrote in
comp.lang.c.moderated:

> Although we have scanned the ANSI-C do***ent, we cannot determine
> whether "prototyping" structures, including a struct's name beyond its
> tag name, is ANSI-C & most likely sup****ted by ANSI-C compliant
> compilers.

The rule is really fairly simple.  A structure type can be declared
without defining it.  Without the definition, it is an incomplete
type.  You can define pointers to incomplete types.

> Similar to prototyping functions, the reason we ask this question is
> to determine whether we can resolve circular definition situations
> that occur when ordering header files which have data type definitions
> each dependent on the other.
> 
> 
> Example Scenario :
> 
> 
>   #include  <types.h>
>   #include  <header_a.h>
>   #include  <header_b.h>
> 
> 
>   In 'types.h' :
> 
>     typedef  struct  some_tag_name  SOME_STRUCT_NAME;

Note that the typedef above is not actually necessary to do what you
want.  But it does serve to tell the compiler that the program might
use a type named "struct some_tag_name", which has an alias of
"SOME_STRUCT_NAME", and if it does, a definition of the type will be
available when needed.

>   In 'header_a.h' :
> 
>     typedef  struct  some_other_struct {
>         ...
>         /* Allowed because of prior prototype? : */
>         SOME_STRUCT_NAME  *psome_struct;

Again, you could do this without the typedef, and indeed even without
a forward declaration at all.  You could just code:

         struct some_tag_name *psome_struct;

That would also tell the compiler that there will be a structure type
named "some_tag_name", and that is all the compiler needs to know to
define an object that can hold a pointer to such a struct.

>         ...
>     } SOME_OTHER_STRUCT;
> 
> 
>   In 'header_b.h' :
> 
>     typedef  struct  some_tag_name {
>         ...
>     } SOME_STRUCT_NAME;
> 
> If SOME_STRUCT_NAME can be "prototyped" in a header PRIOR to
> 'header_a.h', then that structure name can be referenced in other
> header files PRIOR to its actual data type definition.

The C standard requires that all pointers to structure types have the
same size, alignment, and representation.  That means that the
compiler does not need to know any details about a structure type to
know that create a pointer to it.

This is valid C code:

struct x;	/* forward declaration	*/
struct y;	/* forward declaration	*/

struct x { int a; struct y *yp; };
struct y { int c; struct x *xp; };

It is also valid without the first two lines, the forward
declarations.  You can define a pointer to a previously unseen
function type.  The appearance of a previously undefined structure
tag, with the struct keyword, serves as a forward declaration of the
type all by itself.

> We have tried this method on one compiler so
> far & it compiles & builds without additional
> warnings or errors.  However, we'd like some
> input before we consider using it in released
> software.
> 
> If this was a fully ANSI-C compliant method
> to resolve circular data type definitions, it
> would be an immense improvement to our code
> base since it would allow us to remove many
> instances of 'void *' definitions that we
> believe had to be used in order to avoid the
> circular definition problem.
> 
> 
> 
> Thank you for your time & expertise,

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 5 Posts in Topic:
ANSI-C Data Type "Prototyping" & Circular-Referenced Definitions
ericshufro@[EMAIL PROTECT  2008-03-08 12:21:09 
Re: ANSI-C Data Type "Prototyping" & Circular-Referenced Definit
=?ISO-8859-1?Q?Hans-Bernh  2008-03-17 15:20:30 
Re: ANSI-C Data Type "Prototyping" & Circular-Referenced Definit
Jack Klein <jackklein@  2008-03-17 15:20:07 
Re: ANSI-C Data Type "Prototyping" & Circular-Referenced Definit
Jonathan Leffler <jlef  2008-03-17 15:20:43 
Re: ANSI-C Data Type "Prototyping" & Circular-Referenced Definit
Barry Schwarz <schwarz  2008-03-17 15:21:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 16:11:12 CDT 2008.