简体   繁体   English

如何在C的头文件中正确编写extern数组的声明?

[英]How to correctly write declarations of extern arrays in C's header files?

First of all, let me confess that pointer-to-an-array always gets me confused. 首先,让我承认指向数组的指针总是让我感到困惑。 Hence, I am asking this question. 因此,我要问这个问题。

Suppose I want to share a global array of data across my program, for example: 假设我想在整个程序中共享全局数据数组,例如:

static const char * EnumStrings_WIP_Selection_Box[] = { "Rectangular", "Siemens Gauss", "My_Gauss", "Planck-Taper", "Invalid"};

When I try to declare corresponding “extern” command 当我尝试声明相应的“ extern”命令时

extern static const char * EnumStrings_WIP_Selection_Box[];

or 要么

extern static const char * EnumStrings_WIP_Selection_Box[5];

I get the following error 我收到以下错误

error C2159: more than one storage class specified

Also, can someone also tell me the different between these? 另外,有人还能告诉我这些区别吗?

Foo *array[10];
Foo (*array1)[10]; 

Any help would be appreciated. 任何帮助,将不胜感激。

I've assumed you want you array to be "totally" constant, as many people aren't aware of the two-consts on a pointer. 我假设您希望数组“完全”恒定,因为许多人不知道指针上的两个常量。 This means not only can the text pointed to by the array not change, the pointers in the array also cannot be changed to point to new text. 这意味着不仅数组所指向的文本不能更改,而且数组中的指针也不能更改为指向新文本。

You should make sure the data in your array is defined in a source file, or you risk a copy of the data in memory for every source file that includes the header. 您应该确保数组中的数据是在源文件中定义的,否则您将冒着风险为每个包含头文件的每个源文件复制数据的风险。

Header file 头文件

extern const char *const MyText[];

Source file 源文件

extern const char *const MyText[] = { "Hello", "There"; };

Alternatively, the C++ approach 另外,C ++方法

Header file 头文件

extern const std::vector<std::string> MyText;

Source file 源文件

extern const std::vector<std::string> MyText = { "Hello", "There"; };

To force it all in one file (not recommended in a header file) 强制将其全部放在一个文件中(不建议在头文件中使用)

const char *const MyText[] = { "Hello", "There"; };

extern and static are both storage classes, and are mutually exclusive. externstatic都是存储类,并且是互斥的。

Don't specify them together. 不要一起指定它们。 You want: 你要:

extern const char * EnumStrings_WIP_Selection_Box[];

The static conflicts with the extern . staticextern冲突。

Try this: 尝试这个:

.cpp file: .cpp文件:

/* not static */
const char * EnumStrings_WIP_Selection_Box[] = { "Rectangular",
    "Siemens Gauss", "My_Gauss", "Planck-Taper", "Invalid"};

.h[pp] file: .h [pp]文件:

extern const char * EnumStrings_WIP_Selection_Box[];

That said, for the sake of future maintainers, please consider defining your access in terms of methods, not exposed pointers (ie consider adding a char const * const GetWipSelectionByIndex(const std::size_t index) to your public API, instead of exposing an array). 也就是说,为了将来的维护者,请考虑根据方法而不是公开的指针定义访问权限(即考虑将char const * const GetWipSelectionByIndex(const std::size_t index)到公共API中,而不要公开数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM