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

107,721

Solution 1

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

Solution 2

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?

Solution 3

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

Solution 4

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

Share:
107,721
Andrey Pesoshin
Author by

Andrey Pesoshin

Computer fan

Updated on July 20, 2022

Comments

  • Andrey Pesoshin
    Andrey Pesoshin almost 2 years

    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?

    • Ken Wayne VanderLinde
      Ken Wayne VanderLinde almost 13 years
      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
    • A. K.
      A. K. almost 13 years
      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.
    • Andrey Pesoshin
      Andrey Pesoshin almost 13 years
      @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.
    • A. K.
      A. K. almost 13 years
      Then @jahhaj's answer seems to be the most appropriate
    • Dan
      Dan almost 13 years
      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.
  • sbi
    sbi almost 13 years
    Why doesn't this work in C? I thought C had copied const from C++ by, I think C89?
  • jahhaj
    jahhaj almost 13 years
    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++.
  • Andrey Pesoshin
    Andrey Pesoshin almost 13 years
    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".
  • Andrey Pesoshin
    Andrey Pesoshin almost 13 years
    when i define it in source file - it cannot be accessed from main.cc file (in which i include mydriver.h)
  • jahhaj
    jahhaj almost 13 years
    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
  • jahhaj
    jahhaj almost 13 years
    in source file const int array[] = { 1, 2, 3 }; in header file extern const int array[]; you need both (assuming you are writing C)
  • Andrey Pesoshin
    Andrey Pesoshin almost 13 years
    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).
  • Andrey Pesoshin
    Andrey Pesoshin almost 13 years
    yes, external h/source files are in C and main source code is in C++ - that is the way EDK offers to do
  • jahhaj
    jahhaj almost 13 years
    Well that's another complication. Mixed C and C++ means you are also going to need extern "C" declarations, see the new answer I added
  • sbi
    sbi almost 13 years
    @jahhaj: Thanks! I never really worked in C, so I didn't know that.