3.4 struct pointers

In the context of pointer type casting, pointers to structures are no different from pointers to built-in types. This means that we can have the following:

struct X
{
};

struct Y
{
  int i;
  float f;
  char buffer[20];
};

struct X *pX;
struct Y *pY;

// ...

pX = (struct X *)pY;

Afterall, struct X is just a type, as is struct Y. The fact that there is no member field in X is not important, at all.

Note that even for named structures with no member fields, C and C++ still enforces type checking. Each named structure is unique. This means that the following code will generate an error:

struct X {};
struct Y {};

struct X *pX;
struct Y *pY;

// ...

pX = pY;


Copyright © 2006-08-30 by Tak Auyeung