Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Answering your first question, I recommend to encapsulate your structures using opaque pointers (a.k.a Handles).</p> <p>For example, you may declare a handle to a linked list (here MS-like naming):</p> <pre><code>typedef struct linked_list_t* HLINKEDLIST; </code></pre> <p>We assume that linked_list_t is a generic one (composed of void pointers).</p> <p>This way you can hide what a "handle" to a linked list is, or in what form is implemented (information hiding):</p> <pre><code>HLINKEDLIST LinkedListCreate(); LinkedListAdd(LLELEMENT v); LinkedListCopy(HLINKEDLIST dst, const HLINKEDLIST src); </code></pre> <p>Handle subtypes are also commonly defined, such as PHLINKEDLIST (pointer to a linked list handle). </p> <p>Related types can also be defined for convenience (and to use the limited information hiding available in C). e.g: the linked list element type can be defined as </p> <pre><code>typedef void* LLELEMENT; </code></pre> <p>There are good books on Data Structures in C to check. This is good: <a href="http://rads.stackoverflow.com/amzn/click/0201498413" rel="nofollow noreferrer">http://www.amazon.com/Interfaces-Implementations-Techniques-Creating-Reusable/dp/0201498413</a></p> <p>Also note that LLELEMENT is actually compatible with void* so if you are defining other type def as:</p> <pre><code>typedef void* SYSTEMDATA; </code></pre> <p>SYSTEMDATA is compatible with LLELEMENT, so the compiler won't complain on:</p> <pre><code>int QuerySystemData(SYSTEMDATA* sd); </code></pre> <p>and calling:</p> <pre><code>QuerySystemData(lle); </code></pre> <p>where lle is of type LLELEMENT.</p> <p>This type checking can be enforced encapsulating simple members in structs. If I don't remember bad, declaring STRICT in a program using windows.h causes handles to be type-safer (incompatible between). Definitions like the following are common:</p> <pre><code>typedef struct __HWND { int __handle; } __HWND; typedef __HWND* HWND; </code></pre> <p>If the simpler definitions were:</p> <pre><code>typedef int HWND; typedef int HBITMAP; </code></pre> <p>The two handles would be type compatible and interchangeable on functions expecting to work with windows and functions expecting to work with bitmaps (potential horrible bugs).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload