0

I will try to describe my problem as best in English first starting from the top:

This starts with a structure named 't' which there needs to be 6 of (this never changes), which contains some simple variables - but also needs to contain an array of structures called 'p' which there needs to be 4 of per 't' object. 'p' will contain some further simple variables followed by a further array of structures called 'f' which will then finally just contain some simple variables.

See code below for how I would attempt this problem:

struct t
{
//some simple variables here
struct p pts[4]; //will always contain 4 elements/objects
};

struct p
{
//more simple variables here
struct f fbs[X]; // x can be any value between 1-4
};

struct f
{
//finally some more simple variables
};

To start off I am just trying to use:

struct p
    {
    //more simple variables here
    struct f fbs[X]; // x can be any value between 1-4
    };

    struct f
    {
    //finally some more simple variables
    };

To get started. However anytime I try and put an array of structures of 'f' or just a single structure 'f' I get an error: "exit status 1 - field 'fbr' has incomplete type 'f'"

Unless I initialize it within 'p' as:

struct f;

Which is not what I want. Any advice / help would be appreciated. Can't seem to find anything understandable online regarding this. I understand it may be to to with memory allocation but am not 100% sure.

Thanks in advance

2
  • One thing Arduino abstracts that I really think does more harm than good is the automatic prototype generation of functions. This makes new programmers think that they don't HAVE to declare things before using them, and then leads to issues like this where you assume you can do the same thing with other concepts like structs. I'd recommend declaring function prototypes before calling them anyway, just to get in the habit of it. Commented Jul 19, 2020 at 13:58
  • That's a very good point because that's exactly where it led me. Thank you, it's a practice I will put into place from now Commented Jul 19, 2020 at 14:13

1 Answer 1

2

The problem here is of the order of declaration/definitions.

You need to at least have the type declared before the compiler can understand and use it (which then would be an "incomplete type", since it's not yet defined. These incomplete type can only be used in certain cases, like declaring pointers to them etc). In your case however it needs to be defined since it is ODR used.

struct p
{
//more simple variables here
struct f fbs[X]; // <---------- what is struct f? Don't know at this point
}; 


// struct f defined here after it's use
struct f
{
//finally some more simple variables
};

A simple fix would be to move the definition of struct f before struct p, and both before struct t

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

4 Comments

Wow, incredibly simple! Thank you! I was not familiar with ODR so that will also make for good reading.
This solution has worked, but I would like to ask a question regarding: struct f fbs[X]; What if X is a variable that is entered at the top level of the structure (i.e. at 't'). Is this where I would need to use memory allocation?
You need variable array size? In that case, you will need to dynamically allocate it. Or, if you know max size and don't mind the memory waste, you could define array of max size, and keep some check on valid range ( maybe size parameter or a sentinel value)
Yes so my array size can be either 1,2 or 4 depending on runtime conditions. So pre-processor directives are gone out the window. So dynamic allocation is the way to go?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.