Why declare a struct that only contains an array in C?

CArraysCoding StyleStruct

C Problem Overview


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?

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

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!

Solution 4 - C

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.

Solution 5 - C

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 :)

Solution 6 - C

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.

Solution 7 - C

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 typedefs 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoe.ZView Question on Stackoverflow
Solution 1 - CBlagovest BuyuklievView Answer on Stackoverflow
Solution 2 - CR.. GitHub STOP HELPING ICEView Answer on Stackoverflow
Solution 3 - CBo PerssonView Answer on Stackoverflow
Solution 4 - CMetallicPriestView Answer on Stackoverflow
Solution 5 - CHossein MobasherView Answer on Stackoverflow
Solution 6 - CAniketView Answer on Stackoverflow
Solution 7 - CPJTraillView Answer on Stackoverflow