Will Dyson on Sun, 8 Dec 2002 20:13:05 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] C question: struct pointer error


Jeff Abrahamson wrote:

   int foo(struct bar *b);

   typedef struct bar {
	    int a;
   } Bar;

   foo(Bar *b)
   {
	    return 0;
   }

My read of K&R is that, as long as you don't need to know the size of
the struct pointer, you don't need to have the definition of the
struct available in the current translation unit.

The real issue here, since I could just #include bar.h, is that I
don't want to #include extraneous files. It slows compilations, risks
breaking data abstraction, and risks creating circular dependencies.

What you need is a forward declaration of struct bar; You are correct that it does not need the definition, but it must know that struct bar is declared.


    struct bar; // forward declaration of struct bar

    int foo(struct bar *b);

    struct bar {
	    int a;
    };

    int foo(struct bar *b)
    {
	    return 0;
    }


-- Will Dyson "Back off man, I'm a scientist!" -Dr. Peter Venkman

_________________________________________________________________________
Philadelphia Linux Users Group        --       http://www.phillylinux.org
Announcements - http://lists.netisland.net/mailman/listinfo/plug-announce
General Discussion  --   http://lists.netisland.net/mailman/listinfo/plug