Defining a 2D array in a separate file

Hi,

I am writing a code which uses five fairly large 2D arrays of chars. Since they are pretty large, I want to have them declared and defined in a separate .h and .cpp file (?), so that they don't clutter up my implementation file but are still accessible from the implementation file just as if they were defined there.

I have been trying to do this for a couple hours now, but I'm still not sure what I'm doing. Any help would be greatly appreciated! For example, maybe I could have a quick little example of how this is done? Thanks!


The arrays are titled CH1, CH2, CH3, CH4, and CH5.
Here is a small sample of the code that defines the arrays:

1
2
3
4
5
6
7
8
9
10
11
//   0
CH1[16][1] = '1';  CH1[16][2] = '1';  CH2[16][0] = '1';  CH2[16][3] = '1';  CH3[16][0] = '1';  CH3[16][3] = '1';  CH4[16][0] = '1';  CH4[16][3] = '1';  CH5[16][1] = '1';  CH5[16][2] = '1';  
 
//   1
CH1[17][2] = '1';  CH2[17][1] = '1';  CH2[17][2] = '1';  CH3[17][2] = '1';  CH4[17][2] = '1';  CH5[17][0] = '1';  CH5[17][1] = '1';  CH5[17][2] = '1';  CH5[17][3] = '1';  

//   2
CH1[18][1] = '1';  CH1[18][2] = '1';  CH2[18][0] = '1';  CH2[18][3] = '1';  CH3[18][3] = '1';  CH4[18][2] = '1';  CH5[18][0] = '1';  CH5[18][1] = '1';  CH5[18][2] = '1';  CH5[18][3] = '1';  

//  ... Many more lines ... 



Thanks in advance,
- Messmerd
Last edited on
In the header (say, my_arrays.h):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef MY_ARRAYS_H_INCLUDED
#define MY_ARRAYS_H_INCLUDED

#include <cstddef>

constexpr std::size_t NROWS = 20 ;
constexpr std::size_t NCOLS = 30 ;

extern char array_CH1[NROWS][NCOLS] ;

// etc.


#endif // MY_ARRAYS_H_INCLUDED 


In the implementation file (say my_arrays.cpp):
1
2
3
4
5
6
7
8
9
10
#include "my_arrays.h"

char array_CH1[NROWS][NCOLS] =
{
    { '1', '1', '0', /* ... */ },
    { '1', /* ... */ },
    // etc.
};

// etc. 


in other files, #include "my_arrays.h" and use them normally.
Thanks for your help!

But one more thing...Is there a way to set the value at every index of each of the five arrays to '0' as a default without resorting to hundreds of lines of code? I know I can do this easily with a for loop, but I don't know how to implement one in the my_arrays.cpp file.

After setting the default values, I'd like to continue using the format:
1
2
//   0
 CH1[16][1] = '1';  CH1[16][2] = '1';  CH2[16][0] = '1';  CH2[16][3] = '1';  CH3[16][0] = '1';  CH3[16][3] = '1';  CH4[16][0] = '1';  CH4[16][3] = '1';  CH5[16][1] = '1';  CH5[16][2] = '1';  


if I can, because changing everything would take a lot of work.

But if there isn't a way to do this, that's ok.
Last edited on
Assuming that there is no other access to (initialised) elements of the array before the main() has been entered into:

my_arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "my_arrays.h"

static bool init_CH1()
{
    // set every element to '0'
    for( auto& row : array_CH1 ) for( char& c : row ) c = '0' ;
    return true ;
}

static const bool init_helper = init_CH1() ;

char array_CH1[NROWS][NCOLS] = {} ;

// etc. 
Topic archived. No new replies allowed.