22

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?

5
  • A macro based solution as used in the first example wouldn't work. Somewhere, an actual array must be allocated in memory, as in sbi's answer Commented Jul 31, 2011 at 22:04
  • 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

34

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?

Sign up to request clarification or add additional context in comments.

6 Comments

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".
when i define it in source file - it cannot be accessed from main.cc file (in which i include mydriver.h)
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
in source file const int array[] = { 1, 2, 3 }; in header file extern const int array[]; you need both (assuming you are writing C)
yes, external h/source files are in C and main source code is in C++ - that is the way EDK offers to do
|
23

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 

Comments

8

In 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++.

4 Comments

Why doesn't this work in C? I thought C had copied const from C++ by, I think C89?
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++.
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).
@jahhaj: Thanks! I never really worked in C, so I didn't know that.
3

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

Comments

1

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:

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

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

byte list[] = {MYARRAY} ;

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

Comments

0

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 

Comments

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++. Works on my computer anyway.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.