An array of structres in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types.
A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure.
Structure object can be created after declaration later in the program. On other hand in case of Array we can't create its object after declaration. Structure supports multiple data-type variables as input.
Arrays are used when there is a need to use many variables of the same type. It can be defined as a sequence of objects which are of the same data type. It is used to store a collection of data, and it is more useful to think of an array as a collection of variables of the same type. Arrays can be declared and used.
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.
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 with
typedef char ABC[MAX];
but 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 type ABC
) or else you'll get stung by the fact that ABC
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.
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!
You can copy it like this.
struct ABC a, b;
........
a = b;
For an array you would need to use memcpy function or a loop to assign each element.
You can use struct to make a new type of data like string. you can define :
struct String {
char Char[MAX];
};
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 of using such a struct
is that it enforces type-safety wherever such a struct
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 a typedef
for it: this has some of the advantages of the struct
– • 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. Two typedef
s for bare arrays of a given type only yield different types if they are of different sizes. Moreover, if you use the typedef
without *
in a function argument, it is equivalent to char *
, drastically reducing type-safety.
In summary:
typedef struct A_s_s { char m[113]; } A_s_t; // Full type safey, assignable
typedef char A_c_t[113]; // Partial type-safety, not assignable
A_s_t v_s(void); // Allowed
A_c_t v_c(void); // Forbidden
void s__v(A_s_t); // Type-safe, pass by value
void sP_v(A_s_t *); // Type-safe
void c__v(A_c_t); // UNSAFE, just means char * (GRRR!)
void cP_v(A_c_t *); // SEMI-safe, accepts any array of 113
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