I've seen some C code that creates a structure and within the structure there are a number of arrays. Some of those arrays are of size one. So why bother making it an array? Why not just a single int?
I'm talking about something like this:
struct Foo
{
uint8_t Bar[1];
uint32_t BigBar[4];
};
Why not make it just
struct Foo
{
uint8_t Bar;
uint32_t BigBar[4];
};
The answer is that it is good programming habit to do so from two reasons:
In case the programmer decides to change the Bar into an array at some point, there is not a lot of code changing. All needed to be done is to change the constant from 1 to ARRAY_SIZE (it is even better to have the constant defined as one actually)
Using fields which are constructed the same are less prone to mistakes than fields which are different. Thinking programmers are ones who make mistakes :)
Cheers
My guess is for an easier return of a pointer:
it will be easier to return the pointer to the variable:Bar
if you will want to use it as a pointer you would be able to pass Bar instead of &Bar if it was an int
if I had an instance of the struct defined: struct Foo aaa;
you would be able to define:
int *pInt = aaaa.Bar;
instead of:
int *pInt = &(aaaa.Bar);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With