[comp.lang.c] self contained pointers to structures

bobc@attctc.Dallas.TX.US (Bob Calbridge) (11/27/89)

Here's one that confounded me.  I wanted to link a number of records of the
same type.  Each record would have the same structure and would contain a
pointer to the next record.  I tried to implement it like this:

struct event {
	int	timing:
	int	count;
	struct	event *next;
};

Of course, the compiler complains because the definition of the structure is
incomplete at that point.  From a practical point of view this shouldn't matter since the space into which the record will go be allocated memory.  Does this
mean that I can define the pointer 'next' as char * and then do some coercion
in my code. I won't be using these structures in an array so all I feel
I really need is some pointer into memory.  

Or should I define a similar structure and reference a pointer to it?  This
does present the problem of what the struct * would have to be in that
definition.

I suspect that this subject has been discussed before but it didn't have my
attention at the time.

bobc@attctc
-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=             I know it's petty..........                                     =
-                  But I have to justify my salary!                           -
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

ark@alice.UUCP (Andrew Koenig) (11/27/89)

In article <10350@attctc.Dallas.TX.US>, bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
> I tried to implement it like this:

> struct event {
> 	int	timing:
> 	int	count;
> 	struct	event *next;
> };

> Of course, the compiler complains because the definition of the structure is
> incomplete at that point.

If your compiler rejects this, it's badly broken.

There's nothing wrong with declaring a pointer to an (as yet)
unknown structure.
-- 
				--Andrew Koenig
				  ark@europa.att.com

peter@ficc.uu.net (Peter da Silva) (11/27/89)

In article <10350@attctc.Dallas.TX.US> bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
> struct event {
> 	int	timing:
> 	int	count;
> 	struct	event *next;
> };

This should work. Your compiler is broken.
-- 
`-_-' Peter da Silva <peter@ficc.uu.net> <peter@sugar.lonestar.org>.
 'U`  --------------  +1 713 274 5180.
"The basic notion underlying USENET is the flame."
	-- Chuq Von Rospach, chuq@Apple.COM 

henry@utzoo.uucp (Henry Spencer) (11/28/89)

In article <10350@attctc.Dallas.TX.US> bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
>struct event {
>	int	timing:
>	int	count;
>	struct	event *next;
>};
>
>Of course, the compiler complains because the definition of the structure is
>incomplete at that point...

Defective compiler.  A pointer to an incomplete type is perfectly proper,
although a variable of the type itself isn't.  (Especially not in this
case, of course!)  Your struct is quite normal and common C practice.
-- 
That's not a joke, that's      |     Henry Spencer at U of Toronto Zoology
NASA.  -Nick Szabo             | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/28/89)

In article <10350@attctc.Dallas.TX.US> bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
>struct event {
>	struct	event *next;
>};
>Of course, the compiler complains because the definition of the structure is
>incomplete at that point.

What do you mean, "of course"?  That is a perfectly valid C construct and
is in fact quite common.

Some deficient compilers do have a problem with such constructs, and there
are a number of work-arounds possible.  One is

struct event;	/* incomplete type to get entry into symbol table */
struct event {
	struct event *next;	/* should be accepted now */
};

john@chinet.chi.il.us (John Mundt) (11/28/89)

In article <10350@attctc.Dallas.TX.US> bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
>Here's one that confounded me.  I wanted to link a number of records of the
>same type.
>
>struct event {
>	int	timing:
>	int	count;
>	struct	event *next;
>};
>
>Of course, the compiler complains because the definition of the structure is
>incomplete at that point.


A perfectly legal construct.  However, notice the colon rather than
the semicolon after the first line

	int timing:

I realize that this was typed for mailing and the typo may not exist
in the original, but there may be some other typo that is squirreling
things.  Look real carefully.

Some older compilers get sick if the same name is used twice
in two different structures, and that may be causing the problem as
well.
-- 
---------------------
John Mundt   Teachers' Aide, Inc.  P.O. Box 1666  Highland Park, IL
john@admctr.chi.il.us *OR* fred@teacha.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem