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

Please elaborate a bit. Does constant array mean that you don't want the array to be changed at a later stage or something else.

Commented Jul 31, 2011 at 22:11

@Aditya Kumar - that's correct. I want to define the set of constants in separate file(s) and access them using integer index from main source file.

Commented Jul 31, 2011 at 22:17 Then @jahhaj's answer seems to be the most appropriate Commented Jul 31, 2011 at 22:44

The Xilinx tag isn't going to help you. I would suggest dropping it, and adding "const" and "array" tags, although at this point the question has been answered sufficiently anyway.

Commented Aug 3, 2011 at 15:54

7 Answers 7

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

const int my_array[] = ; 

Do you have any reason to assume that there would be some problem on that embedded platform?

468k 64 64 gold badges 825 825 silver badges 1k 1k bronze badges answered Jul 31, 2011 at 22:00 223k 46 46 gold badges 263 263 silver badges 445 445 bronze badges

okay. And where to define it - header/source file? When i define it in third-party h file (it's a separate driver for microprocessor system) - i get an error - something like "this variable cannot be defined multiple times".

Commented Jul 31, 2011 at 22:11

when i define it in source file - it cannot be accessed from main.cc file (in which i include mydriver.h)

Commented Jul 31, 2011 at 22:11

So you are writing in C? You have managed to pick on the the few areas where the same code will work differently in C and C++, so please specify which language

Commented Jul 31, 2011 at 22:13

in source file const int array[] = < 1, 2, 3 >; in header file extern const int array[]; you need both (assuming you are writing C)

Commented Jul 31, 2011 at 22:13

yes, external h/source files are in C and main source code is in C++ - that is the way EDK offers to do

Commented Jul 31, 2011 at 22:19

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 
answered Jul 31, 2011 at 22:26 793 4 4 silver badges 9 9 bronze badges
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++.

answered Jul 31, 2011 at 22:01 793 4 4 silver badges 9 9 bronze badges Why doesn't this work in C? I thought C had copied const from C++ by, I think C89? Commented Jul 31, 2011 at 22:03

If this was put in a header file and that header file was included in more than one source file, in C you would get multiple definition errors, in C++ you would not. Const does not have exactly the same meaning in C and C++.

Commented Jul 31, 2011 at 22:05

to be specific - my application consists in general of 2 parts - main program written in C++ and peripheral core software driver written in pure C (as it is compiled as a part of board support package).

Commented Jul 31, 2011 at 22:14 @jahhaj: Thanks! I never really worked in C, so I didn't know that. Commented Jul 31, 2011 at 22:36

It's impossible to define an array constant using the #define directive.

4,801 9 9 gold badges 66 66 silver badges 141 141 bronze badges answered Jul 31, 2011 at 22:02 7,867 4 4 gold badges 45 45 silver badges 71 71 bronze badges

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] = ; 

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 
answered Jul 7, 2014 at 7:19 51 5 5 bronze badges
#include #include #define defStrs new string[4] < "str1","str2","str3","str4" >using namespace std; . const string * strs = defStrs; string ezpzStr = strs[0] + "test" + strs[1]; cout  

Took me a while to figure this out, but apparently it works like this in C++. Works on my computer anyway.

answered Apr 20, 2019 at 22:29

Only try to understand what the compiler does. When it finds the HASHTAG , it replaces its content into the place where it's writed. So for example, if u do this:

byte list[] = MYARRAY ;

You will get it in the array 'list' . Another way to get the same result is:

#define MYARRAY 5, 6, 7, 8

In conclusion, the compliler what does is 'cutting' the hashtag and 'pasting' the content of it. I hope I had solved your doubt