3.3 Self pointers?

This is the interesting part. The field of a structure can point to something of the containing type. It is better to illustrate this with an example:

struct X
{
  int i;
  struct X *pX;
};

It may look confusing. How can a pointer point to a structure that is not even fully defined, yet? The definition may seem recursive. The key to understand this is to look at things from the perspectives of the compiler.

As it turns out, all pointers are implemented the same way. To a processor, there is no difference between a pointer to an integer, a character or a struct X. The ``typing'' of pointers is only there so that a compiler can perform type checking. In other words, a compiler checks to make sure the way a pointer is used is consistent throughout a program.

From this perspective, there is nothing special about the structure definition. All we are saying is that the field pX can only point to a structure of type struct X. It doesn't matter if we are in the process of defining struct X.

We'll be using a lot of structure definitions that contain pointers to its own type.

Copyright © 2006-08-28 by Tak Auyeung