How to get Ada private types in C

congdon at ci-dandelion.UUCP congdon at ci-dandelion.UUCP
Thu Mar 12 04:28:21 AEST 1987


In article <172 at m10ux.UUCP> mnc at m10ux.UUCP (MHx7002 ) writes:
>Wouldn't it be nice if the same feature could be obtained in a C header file
>(which is the analog of an Ada specification)?  Well the following program
>compiles without errors on Berkeley 4.3 and Sys V.2 (VAX versions):
>
>	typedef struct PRIVATE_S *PRIVATE_P;
>	main()
>	{ PRIVATE_P x; if (x) exit();
>	}
>
>If this is legal C everywhere, it implies that one can obtain a private type
>by declaring it to be a struct pointer in the header file (PRIVATE_P), but
>only declaring the struct type (PRIVATE_S) in the module that owns the type.
>Comments?

I'm not sure if this is 'legal' C or not (any comments from the
language lawyers?) but lint (with -h switch) will warn about the
missing definition for 'struct PRIVATE_S'. The structure tag construct
is intended to allow definition of self-referential or
forward-referencing struct types (K&R pg. 197). For example:

	typedef struct Foo { 
	    struct Foo *next;
	    /* other stuff ... */
	} FOO;

But in such cases the struct type is eventually defined so the compiler
shouldn't complain. In your example, since the compiler never sees a
definition for the struct type it's reasonable for the compiler (or
lint) to warn about the missing struct type.

Pascal allows usage of a pointer types in type definitions before the
base type of the pointer type has been defined for self-referential
types as well.  In fact, I think that this is the only example of
'usage before definition' in Standard Pascal (where even labels have to
be pre-defined!).

-- 
Robert M. Congdon    UUCP: {talcott,vaxine,mit-eddie}!ci-dandelion!congdon
Cognition, Inc.      PHONE: (617) 667-4800
900 Tech Park Drive		
Billerica, MA 01821		



More information about the Comp.lang.c mailing list