NO

Author Topic: how to avoid ugly pointer to your own data structure.  (Read 3771 times)

JosefM

  • Guest
how to avoid ugly pointer to your own data structure.
« on: March 26, 2010, 01:32:21 PM »
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
Code: [Select]
//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:

Code: [Select]
//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 ;).
« Last Edit: March 26, 2010, 01:38:46 PM by JosefM »