4

I am working on an embedded system and I need to implement a linked list.

So I used a struct to construct a node

typedef struct A
{
   ... //some data
   struct A *next;
   struct A *prev;
} A;

I think on PC (gcc) this works fine. However, the embedded system compiler complains that "identifier A is not declared"...

What is the best solution for this?

1
  • 1
    Out of curiosity, which embedded compiler are you using? Commented Apr 20, 2012 at 9:13

3 Answers 3

10

You should add a separate forward declaration of the struct:

struct A;
typedef struct A
{
    ... //some data
    struct A *next;
    struct A *prev;
} A;

Some compilers do take your definition the way you posted it, but I've seen older compilers that require a separate forward declaration. This may be related to an older standard, or an incomplete standard implementation. In fact, on a project where we needed to write code that runs on five platforms with different compilers, we made it a companywide coding standard requirement to have the forward declaration separate from the struct's typedef.

Sign up to request clarification or add additional context in comments.

Comments

1

You can split it out:

typedef struct A A;

struct A {
   ... //some data
   struct A *next;
   struct A *prev;
};

Comments

0

The original code should compile just fine. You are probably using a non-standard compiler. As an alternative to the other suggestions, you can try this code:

typedef struct a
{
   ... //some data
   struct a *next;
   struct a *prev;
} A;

This way, you shouldn't need a forward declaration.

1 Comment

Oh btw, if you compile the original code in a C++ compiler, it won't work. C++ handles struct namespaces different than C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.