ARM Technical Support Knowledge Articles

FORWARD STRUCTURE REFERENCES IN C

Applies to: General Topics

Answer


Information in this article applies to:


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 };

Article last edited on: 2000-04-27 00:00:00

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential