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

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
  • 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. – JohnFilleau Jul 19 '20 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 – Brandon Kellett Jul 19 '20 at 14:13

1 Answers1

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

User 10482
  • 855
  • 1
  • 9
  • 22
  • Wow, incredibly simple! Thank you! I was not familiar with ODR so that will also make for good reading. – Brandon Kellett Jul 19 '20 at 14:11
  • 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? – Brandon Kellett Jul 19 '20 at 14:23
  • 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) – User 10482 Jul 19 '20 at 15:19
  • 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? – Brandon Kellett Jul 20 '20 at 07:02