为什么要声明一个只包含C中的数组的结构?

时间:2022-09-06 11:19:21

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 个解决方案

#1


179  

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.

结构可以通过值传递,不像数组在这些上下文中会衰减到指针。

#2


79  

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

另一个优点是它可以抽象出大小,这样您就不必在您声明此类对象的任何地方使用[MAX]了。这也可以通过

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.

但还有一个更大的问题:你必须知道ABC是数组类型(即使你看不到这当你声明变量类型的ABC),否则你会受到这一事实ABC将意味着不同的东西在一个函数参数列表和变量声明/定义。

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.

另外一个好处是,如果需要,可以使用struct添加更多元素,而无需重写大量代码。

#3


44  

You can copy a struct and return a struct from a function.

您可以复制一个struct并从函数返回一个struct。

You cannot do that with an array - unless it is part of a struct!

你不能对一个数组这样做——除非它是一个结构体的一部分!

#4


25  

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.

对于数组,您需要使用memcpy函数或循环来分配每个元素。

#5


10  

You can use struct to make a new type of data like string. you can define :

可以使用struct创建一种新的数据类型,比如string。您可以定义:

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


1  

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.

结构可以包含数组初始化、复制和fini函数,模拟OOP内存管理范例的一些优点。事实上,很容易扩展这个概念来编写通用的内存管理实用程序(通过使用sizeof()结构来确切地知道管理了多少字节)来管理任何用户定义的结构。许多用C编写的智能产品代码都大量使用这些代码,除非数组的作用域非常局部,否则通常不会使用数组。

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.

实际上,对于嵌入在结构中的数组,您可以做其他“智能的事情”,比如在您想要访问这个数组时进行绑定检查。同样,除非数组作用域非常有限,使用它并在程序之间传递信息是一个坏主意。迟早,你会遇到一些让你晚上睡不着觉的臭虫,破坏你的周末。

#7


1  

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.

如果不包装在一个结构体数组,你仍然可以声明一个类型定义:这有一些结构的优点——•类型声明一次,•大小自动正确,代码变得更加清晰的意图,•和代码更易于维护,但你失去◦严格的类型安全,◦能够复制和返回值的类型和◦添加成员的能力后在不破坏代码的其余部分。对于给定类型的裸数组,两个类型只有在大小不同的情况下才产生不同的类型。此外,如果在函数参数中使用没有*的typedef,那么它就相当于char *,从而大大降低了类型安全性。

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

#1


179  

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.

结构可以通过值传递,不像数组在这些上下文中会衰减到指针。

#2


79  

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

另一个优点是它可以抽象出大小,这样您就不必在您声明此类对象的任何地方使用[MAX]了。这也可以通过

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.

但还有一个更大的问题:你必须知道ABC是数组类型(即使你看不到这当你声明变量类型的ABC),否则你会受到这一事实ABC将意味着不同的东西在一个函数参数列表和变量声明/定义。

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.

另外一个好处是,如果需要,可以使用struct添加更多元素,而无需重写大量代码。

#3


44  

You can copy a struct and return a struct from a function.

您可以复制一个struct并从函数返回一个struct。

You cannot do that with an array - unless it is part of a struct!

你不能对一个数组这样做——除非它是一个结构体的一部分!

#4


25  

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.

对于数组,您需要使用memcpy函数或循环来分配每个元素。

#5


10  

You can use struct to make a new type of data like string. you can define :

可以使用struct创建一种新的数据类型,比如string。您可以定义:

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


1  

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.

结构可以包含数组初始化、复制和fini函数,模拟OOP内存管理范例的一些优点。事实上,很容易扩展这个概念来编写通用的内存管理实用程序(通过使用sizeof()结构来确切地知道管理了多少字节)来管理任何用户定义的结构。许多用C编写的智能产品代码都大量使用这些代码,除非数组的作用域非常局部,否则通常不会使用数组。

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.

实际上,对于嵌入在结构中的数组,您可以做其他“智能的事情”,比如在您想要访问这个数组时进行绑定检查。同样,除非数组作用域非常有限,使用它并在程序之间传递信息是一个坏主意。迟早,你会遇到一些让你晚上睡不着觉的臭虫,破坏你的周末。

#7


1  

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.

如果不包装在一个结构体数组,你仍然可以声明一个类型定义:这有一些结构的优点——•类型声明一次,•大小自动正确,代码变得更加清晰的意图,•和代码更易于维护,但你失去◦严格的类型安全,◦能够复制和返回值的类型和◦添加成员的能力后在不破坏代码的其余部分。对于给定类型的裸数组,两个类型只有在大小不同的情况下才产生不同的类型。此外,如果在函数参数中使用没有*的typedef,那么它就相当于char *,从而大大降低了类型安全性。

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