Search code examples
c++carraysconstants

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?


Solution

  • 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