Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining self-referential structs in a C header file (.h)?

Tags:

c

header

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
like image 486
Tim Greene Avatar asked Dec 18 '11 00:12

Tim Greene


People also ask

What is self-referential structures in C?

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.

Should you define struct in header file?

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.

What is self-referential structure give an example?

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.

Where should structures be defined in C?

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.


2 Answers

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?).

like image 51
Dmitri Avatar answered Sep 23 '22 07:09

Dmitri


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;
};
like image 20
Kevin Avatar answered Sep 19 '22 07:09

Kevin