how to avoid ugly pointer to your own data structure.

Started by JosefM, March 26, 2010, 01:32:21 PM

Previous topic - Next topic

JosefM

Just found this and I want to share it with you.
I've tested it and it works on PellesC, VC++6, and VC++7.1

//An ugly working code
typedef struct __Element
{
long                     number;
long                       size;
struct __Element*          next;     //<-- ugly pointer to our own data structure
struct __Element*          prev;
}Element, *pElement;


Guess what, we can replace it with a much better one. Here it is:


//A better version
typedef struct __Element* pElement;          //<---define this first, so we can use it later.
typedef struct __Element
{
long     number;
long       size;
pElement   next;            //<-- ahh.. much better
pElement   prev;
}Element;



Cheers ;).