简体   繁体   English

如何在c/c++中定义常量数组?

[英]how to define a constant array in c/c++?

How to define constant 1 or 2 dimensional array in C/C++?如何在 C/C++ 中定义常量一维或二维数组? I deal with embedded platform (Xilinx EDK), so the resources are limited.我处理嵌入式平台(Xilinx EDK),所以资源有限。

I'd like to write in third-party header file something like我想在第三方 header 文件中写类似

#define MYCONSTANT 5

but for array.但是对于数组。 Like喜欢

#define MYARRAY(index) { 5, 6, 7, 8 }

What is the most common way to do this?最常见的方法是什么?

In C++ source file 在C ++源文件中

extern "C" const int array[] = { 1, 2, 3 };

In header file to be included in both C and C++ source file 在头文件中包含在C和C ++源文件中

#ifdef __cplusplus
extern "C" {
#endif
extern const int array[];
#ifdef __cplusplus
}
#endif

In C++, the most common way to define a constant array should certainly be to, erm, define a constant array : 在C ++中,定义常量数组的最常用方法当然应该是, 定义一个常量数组

const int my_array[] = {5, 6, 7, 8};

Do you have any reason to assume that there would be some problem on that embedded platform? 你有理由认为嵌入式平台会出现问题吗?

In C++ 在C ++中

const int array[] = { 1, 2, 3 };

That was easy enough but maybe I'm not understanding your question correctly. 这很容易,但也许我没有正确理解你的问题。 The above will not work in C however, please specify what language you are really interested in. There is no such language as C/C++. 以上内容在C中不起作用,请指出您真正感兴趣的语言。没有C / C ++这样的语言。

使用define指令定义array常量是不可能的。

I have had a similar problem. 我遇到过类似的问题。 In my case, I needed an array of constants in order to use as size of other static arrays. 在我的例子中,我需要一个常量数组,以便用作其他静态数组的大小。 When I tried to use the 当我试图使用

const int my_const_array[size] = {1, 2, 3, ... };

and then declare: 然后声明:

int my_static_array[my_const_array[0]];

I get an error from my compiler: 我的编译器收到错误:

array bound is not an integer constant

So, finally I did the following (Maybe there are more elegant ways to do that): 所以,最后我做了以下(也许还有更优雅的方法):

#define element(n,d) ==(n) ? d :
#define my_const_array(i) (i) element(0,1) (i) element(1,2) (i) element(2,5) 0
#include <string>
#include <iostream>
#define defStrs new string[4] { "str1","str2","str3","str4" }
using namespace std;
...

const string * strs = defStrs;
string ezpzStr = strs[0] + "test" + strs[1];

cout << ezpzStr << endl;

Took me a while to figure this out, but apparently it works like this in C++. 我花了一些时间来解决这个问题,但显然它在C ++中的工作原理是这样的。 Works on my computer anyway. 无论如何,在我的电脑上工作。

Only try to understand what the compiler does.只尝试了解编译器的作用。 When it finds the HASHTAG, it replaces its content into the place where it's writed.当它找到 HASHTAG 时,它会将其内容替换到写入它的位置。 So for example, if u do this:因此,例如,如果您这样做:

#define MYARRAY { 5, 6, 7, 8 } #define MYARRAY { 5, 6, 7, 8 }

byte list[] = MYARRAY;字节列表[] = MYARRAY;

You will get it in the array 'list'.您将在数组“列表”中获得它。 Another way to get the same result is:获得相同结果的另一种方法是:

#define MYARRAY 5, 6, 7, 8 #define MYARRAY 5、6、7、8

byte list[] = {MYARRAY};字节列表[] = {MYARRAY};

In conclusion, the compliler what does is 'cutting' the hashtag and 'pasting' the content of it.总之,编译器所做的是“剪切”主题标签并“粘贴”它的内容。 I hope I had solved your doubt我希望我已经解决了你的疑问

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

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