Article View: comp.std.c
Article #4285ptr to struct comapatible with ... ugly standards question...
From: Peter Seebach
Date: Fri, 27 Oct 1995 17:44
Date: Fri, 27 Oct 1995 17:44
45 lines
1960 bytes
1960 bytes
I recently wrote a "generic" linked list sorter. It assumes that
struct foo {
struct foo *next;
};
is "compatible" with any structure beginning with a pointer
to itself. I *believe* this is legal; there can be no initial padding, and I
seem to recall that pointer to struct is compatible with pointer to struct.
I am now doing a doubly-linked list sorter. I am going to assume that, if
the user hands me a list looking like:
struct foo {
struct { struct foo *next, *prev; };
...
};
that it's compatible with my
struct bar {
struct { struct bar *next, *prev; };
};
... I can go to "struct bar *next; struct bar *prev;" if needbe.
Here's where it gets ugly:
Can I assert that treating a struct bar as a struct foo will
work? In other wordss, can I *legally* (Screw real-world compilers; I'm
writing for ANSI, not for a specific project) assert that a pointer to a
struct is good for referencing a first member of that struct *or*
for referencing the first member of a struct which is the first
member of that struct?
And if anyone can point me at a better way to say this in English,
I'd appreciate it.
(My plan is to simply use my sorting engine, which is mergesort based, to sort
the doubly-linked list as if it were a singly-linked list, then iterate
through correcting the prev pointers.)
-s
--
Peter Seebach - seebs@solon.com || seebs@intran.xerox.com --
C/Unix proto-wizard -- C/Unix questions? Send mail for help. No, really!
Copyright 1995 Peter Seebach. -- High energy particle theology
The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq
Technical Details
Message-ID:
<46r5pk$i9d@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: seebs@solutions.solon.com (Peter Seebach) Newsgroups: comp.std.c Subject: ptr to struct comapatible with ... ugly standards question... Date: 27 Oct 1995 12:44:20 -0500 Organization: Usenet Fact Police (Undercover) Message-ID: <46r5pk$i9d@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com