I'm trying to create a struct used in two .c source files to make a simple linked list structure. I thought it would save time to create a struct in the header file, however, I get a 'parse error before *' error.
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 did originally omit a detail that is, I'm actually compiling with xcc from the XMOS toolchain. I still don't understand that there would be a difference in .h
file syntax.
Could it be a compilation flag I'm using?
Here's the console printout:
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
Self Referential Structures. • A structure can have members which point to a structure variable of the same type. • These types of structures are called self referential structures and are widely used in dynamic data structures like trees, linked list, etc.
Structs should be defined in headers, unless they are local to specific file — a special occasion, prefer to define them in header, you can easily move them to implementation file later if needed.
A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a self-referential data structure. The next node of a node is being pointed, which is of the same struct type.
Syntax to Define a Structure in C structName: This is the name of the structure which is specified after the keyword struct. data_Type: The data type indicates the type of the data members of the structure. A structure can have data members of different data types.
Looking around in some of the XMOS documentation, it seems the problem is that XC is not C, it's just a C-like language. From the "XC Programming Guide":
XC provides many of the same capabilities as C, the main omission being support for pointers.
...which explains why it doesn't accept the next
and prev
pointers in your structure.
Apparently xcc lets you mix C and XC sources, though, so if you were to limit your use of the structure to C code it should work. From the "XCC Command-Line Manual", it appears that anything with a .xc
extension (as in the command line you used above) is treated as XC, rather than C, source code by default. This can be overridden by placing the option -xc
before the C sources on the command line and -x
afterward (or just rename the files with a .c
extension).
If you must use XC rather than C, you may need to find another way of doing things (arrays, maybe?).
Try using a 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 doesn't recognize the identifier in the middle of its own definition, I'm not sure what the standards say about that (if anything).
For those that don't already know, this is also the way to deal with circular dependencies in struct
definitions:
struct a;
struct b;
struct a {
int x;
struct b *y;
};
struct b {
int x;
struct a *y;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With