briba
briba

Reputation: 2987

Array of a struct

I'm changing my code to work with arrays instead of a linked list.

The fact is I'm receiving this error:

error: expected identifier or ‘(’ before ‘[’ token

And I'm doing this:

typedef struct MyStage * stage_t;

struct MyStage {
    ...
}

stage_t[] stages = new stage_t[len];

If I declare like this:

struct stage_t stages[len];

I receive an error like this:

error: array type has incomplete element type

What am I doing wrong?

Upvotes: 0

Views: 46

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You can't do this

stage_t[] stages = new stage_t[len];

it's invalid, it's not Java, in c++ you need to declare a pointer instead, like this

stage_t *stages = new stage_t[len];

the other syntax works for array declaration, arrays can be converted to pointers, and they are without any extra code, but when you need a dynamically allocated array you need to use a pointer.

Also, you need to remember to

delete[] stages;

Upvotes: 1

Related Questions