Defining self-referencing structures in a C (.h) header file?

I am trying to create a structure used in two .c source files to create a simple structure of linked lists. I thought this would save time for creating the structure in the header file, however I get the error "parse error before *".

This is the code I'm using:

/* * Structures.h * * Created on: Dec 17, 2011 * Author: timgreene */ #ifndef STRUCTURES_H_ #define STRUCTURES_H_ typedef struct list_struct { int data; struct list_struct* next; struct list_struct* prev; } list; #endif /* STRUCTURES_H_ */ 

Edit: I initially omitted the details that are, I actually compile with xcc from the XMOS toolchain. I still don't understand that there will be a difference in the .h syntax.

Could this be the compilation flag I'm using?

Here's a printout of the console:

 xcc -O0 -g -Wall -c -MMD -MP -MF"filter.d" -MT"filter.d filter.o " -target=XC-1A -o filter.o "../filter.xc" In file included from ../filter.xc:15: Structures.h:13: error: parse error before '*' token Structures.h:14: error: parse error before '*' token Structures.h:15: error: parse error before '}' token 
+8
c header
source share
2 answers

Looking back at some XMOS docs, the problem seems to be that XC is not C , it's just a C-like language. From the "XC Programming Guide":

XC provides many of the same features as C, the main omission being support for pointers.

... which explains why it does not accept next and prev pointers in your structure.

Apparently, xcc allows you to mix C and XC sources, so if you need to limit the use of the structure to C code, it should work. From the β€œXCC Command Line Guide”, it seems that everything that has the extension .xc (as in the command line above) is used by default as the XC source code, not C. This can be overridden by placing the -xc option -xc front of C sources on the command line and -x after (or just rename the files with the extension .c ).

If you should use XC rather than C, you may need to find another way to do things (like arrays).

+10
source share

Try using the forward declaration of struct list_struct :

 struct list_struct; typedef struct list_struct { int data; struct list_struct* next; struct list_struct* prev; } list; 

Perhaps your compiler does not recognize the identifier in the middle of its own definition, I'm not sure what the standards say about it (if any).

For those who don't know yet, this is also a way to solve circular dependencies in struct definitions:

 struct a; struct b; struct a { int x; struct b *y; }; struct b { int x; struct a *y; }; 
+1
source share

All Articles