2

My code looks like this

struct {
    int valid;
    int pid;
    int heading;
    int speed;
} car;

struct car road[10];    //a road filled with cars

However, my compiler is complaining:

"incomplete struct/union/enum car: road"

What am I doing wrong? I looked at examples online and they show that this is how to initialize arrays of structs.

If it isn't obvious, I'm trying to make an array of car structs

1
  • Are you missing a typedef before struct? Commented Feb 21, 2017 at 10:27

3 Answers 3

3

Change :

struct {
    int valid;
    int pid;
    int heading;
    int speed;
} car;

which declares only one instance of an unnamed structure, with the name car, to :

struct car{
    int valid;
    int pid;
    int heading;
    int speed;
};

which declares a new type struct car.The struct's name needs to be next to the keyword struct.


You can also do :

typedef struct {
    int valid;
    int pid;
    int heading;
    int speed;
} car;

and then you only refer to this struct as car, not struct car.

See this link for more information on structs and their syntax.

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

6 Comments

Thanks this works! But.. I'm confused between the differences of declaring a struct with the name before and after the struct keyword. Do you know any tips on when to/differences on both?
@penu What do you mean before struct keyword?
@penu What you wrote defines a variable called car whose type is an unnamed struct. The struct car { ... } version defines a type called struct car. You can combine the two: struct car { ... } my_car; defines both a type struct car and a variable my_car.
Sorry, I mean before like as in how you typed it struct car and after like how I had it struct {..} car;
@penu in your example, 'car' becomes a variable of declared struct type, not the struct's name. That is the difference
|
1

This:

struct {
  ...
} car;

declares a single instance of an anonymous structure, called car. You can view the struct {... } part as analogous with any other type name, except a new type is used. Since you don't give the structure a name, you can't create more than the car instance.

This might seem like a pointless thing to do, but it can be really useful when you want to group things together.

What you meant was this:

struct car {
 ...
};

which declares a new struct type, called struct car. You can use this name to refer to the declaration, and e.g. later create a bunch of instances like you did:

struct car road[10];

1 Comment

You could even combine the two declarations: struct car { ... } road[10]; (probably not a good idea, though).
1

Option 1 (without typedef):

struct car {
    int valid;
    int pid;
    int heading;
    int speed;
};

struct car road[10];

Option 2 (with typedef with unnamed struct):

typedef struct {
    int valid;
    int pid;
    int heading;
    int speed;
} car_t;

car_t road[10];

Option 3 (with typedef with named struct):

typedef struct car {
    int valid;
    int pid;
    int heading;
    int speed;
} car_t;

car_t road[10];
//or
struct car road[10];

The Linux Coding Style states:

It's a mistake to use typedef for structures...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.