Why declare a struct that only contains an array i

2019-01-04 19:30发布

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?

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-04 19:45

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.

查看更多
Ridiculous、
3楼-- · 2019-01-04 19:45

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!

查看更多
Anthone
4楼-- · 2019-01-04 19:46

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.

查看更多
够拽才男人
5楼-- · 2019-01-04 19:58

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

查看更多
唯我独甜
6楼-- · 2019-01-04 20:01

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.

查看更多
别忘想泡老子
7楼-- · 2019-01-04 20:07

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
查看更多
登录 后发表回答