Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define a constant array in c/c++?

How to define constant 1 or 2 dimensional array in C/C++? I deal with embedded platform (Xilinx EDK), so the resources are limited.

I'd like to write in third-party header file something like

#define MYCONSTANT 5

but for array. Like

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

What is the most common way to do this?

like image 236
Andrey Pesoshin Avatar asked Jul 31 '11 21:07

Andrey Pesoshin


2 Answers

In C++, the most common way to define a constant array should certainly be to, erm, define a constant array:

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?

like image 133
sbi Avatar answered Oct 19 '22 20:10

sbi


In C++ source file

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

In header file to be included in both C and C++ source file

#ifdef __cplusplus
extern "C" {
#endif
extern const int array[];
#ifdef __cplusplus
}
#endif
like image 22
jahhaj Avatar answered Oct 19 '22 19:10

jahhaj