Keil Logo

GENERAL: FORWARD STRUCTURE REFERENCES IN C


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.05
  • C251 Version 1.24
  • C251 Version 2.14
  • C51 Version 5.50a
  • C51 Version 6.01

QUESTION

How can I create several structures that reference each other. It appears that there is a problem with C understanding forward recerences to structures. For example, the following code:

struct a
  {
  struct b *pb;
  };

struct b
  {
  struct a *pa;
  };


struct a a1 = { &b1 };
struct b b1 = { &a1 };

Generates the following error:

*** ERROR C202 IN LINE 12 OF .\JJ.C: 'b1': undefined identifier

How do I do this in C?

ANSWER

Change your code to the following to use forward struct references.

struct a
  {
  struct b *pb;
  };

struct b
  {
  struct a *pa;
  };

struct b b1;   /* declare the b1 but don't define it -- this makes it work */
struct a a1 = { &b1 };
struct b b1 = { &a1 };

Last Reviewed: Thursday, April 27, 2000


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.