C programming question ....
Roger Oberholtzer
roger
Wed Nov 8 12:23:16 PST 2006
On Wed, 2006-11-08 at 14:13 -0500, Matthew Carpenter wrote:
> On Wednesday 08 November 2006 11:16, Ben Duncan wrote:
> >
> > And it COMPILES and works. SO, it seems that
> >
> > struct rlib;
> > typedef struct rlib rlib;
> >
> > Is a way to prototype a Structure ...
> >
> > Ok, my questions to the REAL C guru's out there is:
> > Why would you want to do such a thing when simply moving
> > the ACTUAL structure ahead of things would solve having to
> > create extra lines of code protyping the structure ?
>
> It's about scope. Allowing the struct to be defined at the place it most
> makes sense is a good way to manage large projects (like, say, a kernel ;)
>
> The first "struct rlib;" is actually implicitly "extern struct rlib;" and the
> compiler is sated. The struct could then be defined in a header file or some
> other code file.
Not so sure about that. In C, data structure definitions (struct and
typedef struct) do not have a concept of extern. They are simply defined
or undefined. I personally would declare it as:
typedef struct {
int whatever;
} rlib;
After all, when all those disconnected declarations are done, that is
what rlib is. So why mess about? If it needs to refer to itself, try
this:
typedef struct rlib_s {
int whatever;
struct rlib_s *next, *previous;
} rlib;
This compiles on every C compiler I have encountered (GNU, MS, TI, SDCC,
OS/9, HPUX, SUN come immediately to mind). Don't let all those c++ types
tell you this is wrong. We are talking C. It is fully allowed.
--
Roger Oberholtzer
More information about the Linux-users
mailing list