I came across some code containing the following:
struct ABC {
unsigned long array[MAX];
} abc;
When does it make sense to use a declaration like this?
I came across some code containing the following:
struct ABC {
unsigned long array[MAX];
} abc;
When does it make sense to use a declaration like this?
It allows you to pass the array to a function by value, or get it returned by value from a function.
Structs can be passed by value, unlike arrays which decay to a pointer in these contexts.
You can copy a struct and return a struct from a function.
You cannot do that with an array - unless it is part of a struct!
A structure can contain array initialization, copy and fini functions emulating some of the advantages of the OOP memory management paradigms. In fact, it's very easy to extend this concept to write a generic memory management utility (by using sizeof() structure to know exactly how many bytes are being managed) to manage any user defined structure. Many of the smart production code bases written in C use these heavily and typically never use an array unless its scope is very local.
In fact for an array embedded in a structure, you could do other "smart things" such as bound checking anytime you wanted to access this array. Again, unless the array scope is very limited, it is a bad idea to use it and pass around information across programs. Sooner or later, you will run into bugs that will keep you awake at nights and ruin your weekends.
You can use struct to make a new type of data like string. you can define :
or you can create a List of data that you can use it by argument of functions or return it in your methods. The struct is more flexible than an array, because it can support some operators like = and you can define some methods in it.
Hope it is useful for you :)
Another advantage is that it abstracts away the size so you don't have to use
[MAX]
all over your code wherever you declare such an object. This could also be achieved withbut then you have a much bigger problem: you have to be aware that
ABC
is an array type (even though you can't see this when you declare variables of typeABC
) or else you'll get stung by the fact thatABC
will mean something different in a function argument list versus in a variable declaration/definition.One more advantage is that the struct allows you to later add more elements if you need to, without having to rewrite lots of code.
Another advantage of using such a
struct
is that it enforces type-safety wherever such astruct
is used; especially if you have two types consisting of arrays of the same size used for different purposes, these types will help you avoid accidentally using an array inappropriately.If you do not wrap an array in a
struct
, you can still declare atypedef
for it: this has some of the advantages of thestruct
– • the type is declared once, • the size is automatically correct, • the intent of code becomes clearer, • and code is more maintainable – but you lose ◦ strict type-safety, ◦ the ability to copy and return values of the type and ◦ the ability to add members later without breaking the rest of your code. Twotypedef
s for bare arrays of a given type only yield different types if they are of different sizes. Moreover, if you use thetypedef
without*
in a function argument, it is equivalent tochar *
, drastically reducing type-safety.In summary: