Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link list node in C, struct prototype

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?

like image 419
Alfred Zhong Avatar asked Dec 20 '22 23:12

Alfred Zhong


1 Answers

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.

like image 138
Sergey Kalinichenko Avatar answered Dec 24 '22 00:12

Sergey Kalinichenko