self contained pointers to structures 
Author Message
 self contained pointers to structures

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;

Quote:
};

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.


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



Mon, 19 Apr 1993 04:01:00 GMT  
 self contained pointers to structures

Quote:

> 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



Mon, 19 Apr 1993 14:14:00 GMT  
 self contained pointers to structures

Quote:

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

This should work. Your compiler is broken.
--

 'U`  --------------  +1 713 274 5180.
"The basic notion underlying USENET is the flame."



Mon, 19 Apr 1993 14:52:00 GMT  
 self contained pointers to structures

Quote:

>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



Mon, 19 Apr 1993 16:42:00 GMT  
 self contained pointers to structures

Quote:

>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 */

Quote:
};



Mon, 19 Apr 1993 20:47:00 GMT  
 self contained pointers to structures

Quote:

>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

(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  



Mon, 19 Apr 1993 21:08:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Pointers to Structure that contains pointer to Function

2. dereferencing pointer to a structure containing a pointer to int

3. reading a structure containing pointers

4. structure containing pointer to char over the network

5. How Do I fwrite() and fread() A Structure that contains pointers to dynamic data

6. Structure containing pointer problem

7. memory block containing pointers, aka pointer to pointer i believe

8. Marshaling a structure containing a aray of different structures

9. Making a Static Built Release Application Self-Contained ...

10. self-referential structures

11. Self-referential structure

12. PB Exporting a structure from a self-made dll to an application (Win32 MFC)

 

 
Powered by phpBB® Forum Software