Pelles C forum

C language => Tips & tricks => Topic started by: JosefM on March 26, 2010, 01:32:21 pm

Title: how to avoid ugly pointer to your own data structure.
Post by: JosefM 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 ;).